javascript - Calling a function from a ngResource object not working -
lets have mill called 'stuff'.
app.factory('stuff', function($resource) { homecoming $resource('api/v1/stuff/', {}, { getthings: { method: 'get', params: {id: '@id'}, url: 'api/v1/things/:id', isarray: true } }); });
in mill have custom getthings function.
in controller, if this:
$scope.stuffs = stuff.query(); $scope.things = stuff.getthings({id: $scope.stuffs[0].id});
it works, , $scope.things want.
but if this:
$scope.stuffs = stuff.query(); $scope.things = $scope.stuffs[0].$getthings();
i error in angular-resource. ajax phone call happens right url , right info getthings errors out immediatly after getting data, , thing left in $scope.things looks promise:
object {then: function, catch: function, finally: function}
the error in js console is:
typeerror: undefined not function @ http://dev.blah.com/assets/angular-resource/angular-resource.js?body=1:558:27 @ foreach (http://dev.blah.com/assets/angular/angular.js?body=1:326:18) @ $http.then.value.$resolved (http://dev.blah.com/assets/angular-resource/angular-resource.js?body=1:556:17) @ wrappedcallback (http://dev.blah.com/assets/angular/angular.js?body=1:11574:81) @ wrappedcallback (http://dev.blah.com/assets/angular/angular.js?body=1:11574:81) @ http://dev.blah.com/assets/angular/angular.js?body=1:11660:26 @ scope.$eval (http://dev.blah.com/assets/angular/angular.js?body=1:12703:28) @ scope.$digest (http://dev.blah.com/assets/angular/angular.js?body=1:12515:31) @ scope.$apply (http://dev.blah.com/assets/angular/angular.js?body=1:12807:24) @ done (http://dev.blah.com/assets/angular/angular.js?body=1:8380:45)
i can't figure out why isn't working.
i believe promise returned on resource query. seek this:
stuff.query(function(response) { response[0].$getthings(function(things) { $scope.things = things; ); });
or:
stuff.query() .$promise .then(function(stuff) { $scope.stuffs = stuff; homecoming stuff[0].$getthings(); }) .then(function(things) { $scope.things = things; });
javascript angularjs ngresource
No comments:
Post a Comment