How to use promises with Angular.js and $ resource with query string?

What is the best way to handle promises in Angular.js with a resource that expects the query string / parameters passed to it? I saw the $ q work handled in the factory, controller, and router, but I'm not sure how to handle it anyway when the parameters are involved.
So if this is a factory:

angular.module(Animals, ['$resource', '$route', '$location', function($resource, $route, $location) { return $resource('http://thezoo.com/animals', { query: {method: 'GET', isArray: true}}); }]); 

and this is the controller:

 Animals.query({size="med",gender='f'}); 

then what is the best way to deal with promises? Invoking an external resource can take quite a while.

+3
source share
2 answers

Really late to the party, but I found this page on Google. If you want to use a resource, but you might want to get data from your query method before continuing (maybe you are writing this in a solution or something else), you can do it.

 var deferred = $q.defer(); Resource.query({params}, function (response) { someData = response; deferred.resolve(someData); }); return deferred.promise; 
+3
source

It is not clear what you are trying to accomplish.

$resource methods will not return promises. They will return empty arrays or objects that will be filled when the data is returned by the server.

If you need a method that returns a promise, you can write one that uses $http directly.

+1
source

Source: https://habr.com/ru/post/1499214/


All Articles