angular-resource

How to cancel $resource requests

萝らか妹 提交于 2019-11-28 17:15:22
I'm trying to figure out how to use the timeout property of a $resource to dynamically cancel pending requests. Ideally, I'd like to just be able to cancel requests with certain attributes (based on the params sent), but it seems this may not be possible. In the meantime, I'm just trying to cancel all pending requests, and then resetting the timeout promise to allow new requests. The issue seems to be that the $resource configuration only allows a single, static promise for the timeout value. It makes sense how I could do this if I was making individual $http calls, since I could just pass in

Testing $resource services in AngularJS

喜你入骨 提交于 2019-11-28 15:50:11
问题 I am trying to begin writing unit tests for my angular application and hit a stopping block pretty quick as I am unsure of how exactly to mock my service in a testable way. Is there a way to mock the REST call otherwise it would seem like I need to mirror everything within my service in my tests which doesn't seem right to me, but I am rather new to test writing so maybe this is how it is supposed to be accomplished. Any help would be greatly appreciated. My service is as follows: angular

angular http: how to call images with custom headers?

强颜欢笑 提交于 2019-11-28 07:35:58
In the html view, images are displayed like this: <img ng-src="{{element.image.url}}"> element.image.url points to an url like: /rest_api/img/12345678 . This is working fine , images are displayed. Now, I add authentication: Before the user is authenticated, every resources respond with an http error 401, images too. When authentication succeeds, a token is placed in a custom headers and sent with every $http requests, allowing access the resources: $http.defaults.headers.common['Authorization'] = token; This is working fine for Json files loaded with $resource. But the direct links to the

Make angularjs $resource return array of [OO] objects

久未见 提交于 2019-11-28 04:08:16
问题 How to make angularjs $resource return an array of objects derived/prototyped from specified domain object? Here is an example on http://plnkr.co/edit/AVLQItPIfoLwsgDzoBdK?p=preview that processes a set of Note s objects. app.controller('MainCtrl', function($scope, NoteResource) { $scope.name = 'World'; $scope.notes = NoteResource.query(); $scope.spellCheckAllNotes = function() { angular.forEach($scope.notes, function(note) { note.spellCheck(); }); } }); The issue is that $resource returns

AngularJS: Creating multiple factories for every endpoint?

我只是一个虾纸丫 提交于 2019-11-28 02:59:17
following some examples, it appears that we can inject a factory which would contain an endpoint for a rest service like so services.factory('Recipe', ['$resource', function($resource) { return $resource('/recipes/:id', {id: '@id'}); }]); This looks great, but imagine I have other endpoints i.e. /users/:id, and /groups/:id, as you can imagine the number of different endpoints are going to increase. So it is good practice to have a different factory for each endpoint so having .. services.factory('Recipe', ['$resource',............ services.factory('Users', ['$resource',............. services

Angular $resource does not parse correctly an integer response

删除回忆录丶 提交于 2019-11-27 18:23:18
问题 I am using the Angular $resource to make requests to my controllers, implemented on a Spring application. When the controller returns just an Integer value, $resource parse it bad. Inspecting it with Firebug I get something like: Resource { 0="1", 1="9", 2="1", more...} where 191 is just the intger value the server returns. No trouble with others complex object (parsed in JSON by the server). Suggestions? Thanks FB 回答1: I know this is a little old, but I just ran into a similar problem and

Dynamic Resource Headers

雨燕双飞 提交于 2019-11-27 13:09:21
问题 I would like to have service providing a resource as in the following code: angular.module('myApp.userService', ['ngResource']) .factory('UserService', function ($resource) { var user = $resource('/api/user', {}, { connect: { method: 'POST', params: {}, isArray:false } }); return user; } Then when using the connect action, I would like to dynamically pass a HTTP header, meaning that it may change for each call. Here is an example, in the controller, please see the comment in the code : $scope

Send Request Body on $resource

时光怂恿深爱的人放手 提交于 2019-11-27 11:31:27
问题 I take a look on Angular API for $resource and I didn't find some way to send a Request Body to a RESTful service. I know this is possible using $http approach, like here, so, is it also possible to do using $resource ? Apparently this is the options for $resource . action – {string} – The name of action. This name becomes the name of the method on your resource object. method – {string} – HTTP request method. Valid methods are: GET, POST, PUT, DELETE, and JSONP params – {object=} – Optional

How to handle $resource service errors in AngularJS

你离开我真会死。 提交于 2019-11-27 06:17:23
I am making requests to my API and I am using AngularJS $resource module. It's different from $http so I don't know how to handle my errors. My service: var appServices = angular.module('app.services', ['ngResource']); appServices.factory('Category', ['$resource', function($resource){ return $resource('/apicategoryerr/?format=:format', {}, { query: { method: 'GET', params: { format: 'json'}, isArray: true, } }); }]); My Controller: ... Category.query(function(data) { console.log(data); }); ... I want something like this or .. I don't know a way to handle errors if my API is not working..

angular http: how to call images with custom headers?

白昼怎懂夜的黑 提交于 2019-11-27 01:53:12
问题 In the html view, images are displayed like this: <img ng-src="{{element.image.url}}"> element.image.url points to an url like: /rest_api/img/12345678 . This is working fine , images are displayed. Now, I add authentication: Before the user is authenticated, every resources respond with an http error 401, images too. When authentication succeeds, a token is placed in a custom headers and sent with every $http requests, allowing access the resources: $http.defaults.headers.common[