0

A have test application on angular. I try receive data from php page in my model. The response come with this command echo json_encode($arr); from php file and has this format [{"id":"1","name":"first","text":"description"}]. To receive this data in my model need to use only query cause .get give an error. My query in controller: $scope.item = Items.query({id:$routeParams.id}); Now if I want to use this data in my model i need to specify my array item[0].name Thats not problem but when i try to save edited data

 $scope.item[0].$save({id:$scope.item[0].id});

i have an error TypeError: Object # has no method 'push'

What am I doing wrong?

1 Answer 1

0

If you have the resource setup correctly, you should be able to save by simply calling:

$scope.item[0].$save();

However, it doesn't look like you are doing that. When you use query, it normally return an array. As you are querying for a single ID, you should really be using the get method:

$scope.item = Items.get({id:$routeParams.id});
...
// Modify $scope.item as needed
...
$scope.item.$save();

The simplest way to define a resource is to use default actions provided by Angular:

.factory('Items', function($resource) {
    return $resource('/angular/angular-seed/app/questions/php/maintext.php');
});

As per Angular Documentation:

The default set contains these actions:
{ 'get':    {method:'GET'},
  'save':   {method:'POST'},
  'query':  {method:'GET', isArray:true},
  'remove': {method:'DELETE'},
  'delete': {method:'DELETE'} };
Sign up to request clarification or add additional context in comments.

10 Comments

I tried but this give an error TypeError: Object #<Resource> has no method 'push' when the page is loading.
agree but same error TypeError: Object #<Resource> has no method 'push'
Can you share more of your code? can't really tell here the problem might be without the rest of the code. The push error is normally associated with $resource method set with isArray:true, but if you are using get you shouldn't be getting that error.
I think the problem might be in your edit.html where you are creating an array by setting ng-model="item[0].name". Are you actually getting the value you expected in the Edit page? Change that to ng-model="item.name" and try again.
isArray: true in get method resolve the problem with init but same effect - $scope.item[0].$save(); give error
|

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.