Testing $resource services in AngularJS

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

Difference between delete and remove method in $resource?

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

AngularJS: Creating multiple factories for every endpoint?

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(); … }

AngularJS Automatically Syncing Data between Server and Client

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

How to handle $resource service errors in AngularJS

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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)