如何使用Angular.js承诺和$资源的查询字符串? [英] How to use promises with Angular.js and $resource with query string?

查看:78
本文介绍了如何使用Angular.js承诺和$资源的查询字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是承诺处理在Angular.js与希望传递给它的查询字符串/参数资源的最佳方式?我已经看到在工厂,控制器和路由器处理$ q的工作,但我不知道如何处理它在任何情况下,还有的涉及参数时。结果
因此,如果这是工厂:

What's the best way to handle promises in Angular.js with a resource that expects a query string / parameters passed to it? I've seen the job of $q handled in the factory, controller and router but I'm not sure how to handle it in any case when there's parameters involved.
So if this is the factory :

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

和这是控制器:

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

那么应该如何最佳地使用承诺处理?对外部资源的调用可能需要相当长的。

then how should this best be handled using promises? The call to the external resource can take quite long.

推荐答案

真的迟到了,但我发现此页面的谷歌。
如果你想使用资源,但也许你希望你继续(也许你在解决或东西写这)之前,从你的查询方法获取数据,你可以做到这一点。

Really late to the party, but I found this page from Google. If you want to use a resource, but maybe you want to get data from your query method before you continue (maybe you're writing this in a resolve or something), you can do this.

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

这篇关于如何使用Angular.js承诺和$资源的查询字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆