Reputation: 4577
I am trying to use Angular, and would like to play with $resource. What is unclear for me is how resource element is created.
For instance:
var service = angular.module("apiService", ["ngResource"]).factory('User',
['$resource',
function($resource){
return $resource('/users/:userId', {userId:'@id'});
}]
);
I am expecting to have an access to a 'User' object, like this:
User.query();
But User is not defined. Is there a specific namespace where this object is defined?
What would be the minimum running sample to get this working? (not that angular.js and angular-resource.js are loaded)
Upvotes: 0
Views: 435
Reputation: 849
Maybe you forgot to include "apiService", or User in your controller? This works:
angular.module("apiService", ["ngResource"]).factory('User',
['$resource',
function($resource){
return $resource('/users/:userId', {userId:'@id'});
}]
);
angular.module("testApp", ['apiService']).controller('Test', function($scope, User) {
User.query();
});
Fiddle for ref: http://jsfiddle.net/thepeak/2sscyck9/
Upvotes: 1
Reputation: 136144
You simply need to do pass success function in query.
.controller('mainCtrl', function($scope, User){
User.query(function(user){
$scope.user = user; //call when return data
});
})
Or you could also this by resolving $promise object of query
.controller('mainCtrl', function($scope, User){
User.query().$promise.then(function(user){
$scope.user = user; //call when return data
});
})
Upvotes: 1