0

My code: http://plnkr.co/edit/2blxwwyv0gS9GYui7IVn?p=preview

I defined a service:

angular.module('jsonService', ['ngResource']).factory('JsonService', function($resource) {

     var jsonService = $resource('data.json/:id',
          {id: "@id"}, //parameters default
          {
            getAll: { method: "GET", params: {} },
            addNew: { method: "POST", params: { id: ":id"}},
          });
     return jsonService;

  });

I keep getting error when I try to call getAll from my controller. I also tried to add a new object but AddNew simply would not work from the controller.

1 Answer 1

2

Add isArray: true

getAll: { method: "GET", params: {}, isArray: true },

Please take a look at actions parameter from $resources.

isArray – {boolean=} – If true then the returned object for this action is an array, see returns section.

And this is how to post data

non-GET "class" actions: Resource.action([parameters], postData, [success], [error])

For example:

var obj = { "id": "2", "created": "3424324", "updated": "2342666", "name": "Bob" };
JsonService.addNew({ "id": "2"}, obj)
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks man, can you tell me also how to add an object using the resource with AddNew I added in my controller this command { "id": "2", "created": "3424324", "updated": "2342666", "name": "Bob" } and it is not working
@Canttouchit, you can do JsonService.addNew({ "id": "2"}, obj)
My bad: plnkr.co/edit/2blxwwyv0gS9GYui7IVn?p=preview, well still not working
@Canttouchit That will call your API to update not updating the json file. you have to implement the actual API to handle that
So basically what you are saying is that there is no way to avoid testing adding an object without implementing some sort of server?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.