$resource
$resource
is a separate, optional module of AngularJS, built over $http
. It allows to create a javascript object that represents the data model. In this way each operation computed on the object created through $resource, is performed also on the server. $resource should be used instead of $http
each time the web application has to deal with a Restful web service.
First parameter is the URLs for the Restful application, second parameter is default values and third parameter is additional action to add.
var app = angular.module('app', ['ngResource'])
.factory('userService', function($resource) {
return $resource('http://jsonplaceholder.typicode.com/users/:userId', {
userId: '@userId'
}, {
'update': {
method: 'PUT'
}
})
})
.controller('HelloCtrl', ['$scope', 'userService', function($scope, userService) {
$scope.getUsers = userService.query();
console.log($scope.getUsers);
$scope.getUser = userService.get({
userId: 1
});
console.log($scope.getUser);
}]);