使用AngularJS浏览承诺 [英] Using Promises in AngularJS Views

查看:164
本文介绍了使用AngularJS浏览承诺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标记达格利什写了一个有关如何漂亮的小文章使用在AngularJS意见的承诺。
有人问有关此方面的意见的问题,但马克没有回答他们(还)。因为我在问我同样的问题,我会问在计算器上,而不是得到一个答案:

Mark Dalgleish wrote a nice little article about how to use promises in AngularJS views. Some people asked questions about this in the comments, but Mark didn't answer them (yet). Because I'm asking me the same question, I will ask on StackOverflow instead to get an answer:


  1. 如果您使用的意见的承诺,我怎么处理装载/等待指示,因为它们是异步?并承诺有东西像解决或withinRequest财产?

  1. If you use promises in views, how do I handle "loading"/"waiting" indication, because they are async? Does a promise have something like a "resolved" or "withinRequest" property?

如何处理错误?通常情况下,他们将出现在第二个回调,但如果我直接在视图中使用一个承诺,我不处理这种情况。有另一种方式?

How do I handle errors? Normally they would arise in the second callback, but if I use a promise directly in the view I don't handle this case. Is there another way?

感谢您。

推荐答案

编辑:角V1.2的<的href=\"http://docs.angularjs.org/guide/migration#templates-no-longer-automatically-unwrap-promises\">resolution在视图中的承诺是不是默认激活。

as of angular v1.2 the resolution of promise in views is not activated by default.

承诺在视图中自动解析初看起来就像是一个方便的工具,但它需要被理解和认真评估的限制数量。这种方法的最大的问题是,它是AngularJS谁将会回调添加到一个承诺,我们有一点控制权。

The automatic resolution of promises in a view looks like a handy tool at first but it has number of limitations that need to be understood and evaluated carefully. The biggest issue with this approach is that it is AngularJS who will add callbacks to a promise and we've got little control over it.

回答你的问题:

1)如图所示,这是最终AngularJS谁将会添加一个成功/错误回调,所以我们没有太多的控制这里。你可以做的是包装原承诺到自定义一个会跟踪分辨率。但这种节约麻利敲击几下键盘的整个目的。不,还有像'解决'没有的东西。总之 - 有跟踪,将所有承诺的工作进展没有统一的机制。如果你的承诺是 $ HTTP 为基础的,你可以使用拦截器或 pendingRequests 属性以跟踪正在进行的请求。

1) As indicated, it is ultimately AngularJS who will add a success / error callbacks so we don't have much control here. What you could do is to wrap the original promise into a custom one that would track resolution. But this kind of deft the whole purpose of saving few keystrokes. And no, there is no things like 'resolved'. In short - there is no universal mechanism for tracking progress that would work for all promises. If your promises are $http-based you might use interceptors or pendingRequests property to track request in progress.

2)你不能。再次,这是AngularJS,增加了 $解析服务中的处理程序,它看起来像这样: promise.then(功能(VAL){承诺。$$ v = VAL;}); (见code的这里)。你可以看到,只有一个成功回调加入使所有的失败都将被忽略。

2) You can't. Once again, it is AngularJS that adds a handler inside the $parse service and it looks like this: promise.then(function(val) { promise.$$v = val; }); (see code here). You can see that only a success callback are added so all the failures are going to be silently ignored.

这些都不是在视图中自动许决议的唯一限制。的另一个问题是,由函数返回承诺将无法正确解决。例如,如果你重写,像这样的例子:

Those are not the only limitations of the automatic promise resolution in the view. The other problem is that promises returned by a function won't be resolved correctly. For example, if you would rewrite an example like so:

myModule.controller('HelloCtrl', function($scope, HelloWorld) {

  $scope.messages = function() {
    return HelloWorld.getMessages();
  }
});

和尝试使用下面的标记:

and try to use the following markup:

<li ng-repeat="message in messages()"></li>

事情会如预期,这可能会作为一个惊喜。

things would work as expected, which might come as a surprise.

在短期:尽管承诺的自动解析似乎是一个方便的快捷方式具有局限性和非显而易见的行为数量。仔细评估这些并决定是否保存几个按键都是值得的。

In short: while the automatic resolution of promises might seem like a handy shortcut it has number of limitations and non-obvious behaviors. Evaluate those carefully and decide if saving few keystrokes are worth it.

这篇关于使用AngularJS浏览承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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