Send Request Body on $resource
Building on @gargc‘s answer, you can pass parameters and a body to a resource’s method: myResource.save({ param: myParam }, myObject);
Building on @gargc‘s answer, you can pass parameters and a body to a resource’s method: myResource.save({ param: myParam }, myObject);
You can mock the requests made by ngResource like this: describe(‘User’, function () { var mockUserResource, $httpBackend; beforeEach(angular.mock.module(‘myApp’)); beforeEach(function () { angular.mock.inject(function ($injector) { $httpBackend = $injector.get(‘$httpBackend’); mockUserResource = $injector.get(‘User’); }) }); describe(‘getUser’, function () { it(‘should call getUser with username’, inject(function (User) { $httpBackend.expectGET(‘/api/index.php/users/test’) .respond([{ username: ‘test’ }]); var result = mockUserResource.getUser(‘test’); $httpBackend.flush(); expect(result[0].username).toEqual(‘test’); … Read more
I don’t believe there is any difference. Angular source code: angular.module(‘ngResource’, [‘ng’]). factory(‘$resource’, [‘$http’, ‘$parse’, function($http, $parse) { var DEFAULT_ACTIONS = { ‘get’: {method:’GET’}, ‘save’: {method:’POST’}, ‘query’: {method:’GET’, isArray:true}, ‘remove’: {method:’DELETE’}, ‘delete’: {method:’DELETE’} From Eric W. (his edit was rejected before I could approve it): AngularJS by Green & Seshadri warns that the delete method … Read more
It’s a matter of preference. But nothing prevents you from consolidating all your resources inside one factory as in: services.factory(‘Api’, [‘$resource’, function($resource) { return { Recipe: $resource(‘/recipes/:id’, {id: ‘@id’}), Users: $resource(‘/users/:id’, {id: ‘@id’}), Group: $resource(‘/groups/:id’, {id: ‘@id’}) }; }]); function myCtrl($scope, Api){ $scope.recipe = Api.Recipe.get({id: 1}); $scope.users = Api.Users.query(); … }
I’ve searched far and wide and, while I might have missed it, I couldn’t find a solution for this problem: uploading files using a $resource action. Let’s make this example: our RESTful service allows us to access images by making requests to the /images/ endpoint. Each Image has a title, a description and the path … Read more
Two-way binding in AngularJS refers to the data model ($scope) and your view (directives). For instance, if the data changes in your model, the view will automatically update. Likewise, if the user modifies data in the view, your model will automatically update. Interacting with web services is conducted via the $http service module. So to … Read more
If you want to use asynchronous method you need to use callback function by $promise, here is example: var Regions = $resource(‘mocks/regions.json’); $scope.regions = Regions.query(); $scope.regions.$promise.then(function (result) { $scope.regions = result; });
you can pass the error handler as a second parameter toquery. Category.query(function(data) {}, function() {}); EDIT: to make things a bit clearer, some examples: var Resource = $resource(‘/restapi/resource’); Resource.query(function(data) { // success handler }, function(error) { // error handler }); Resource.query({ ‘query’: ‘thequery’ },function(data) { // success handler }, function(error) { // error handler }); … Read more