如何获得承诺的价值已经解决了吗? [英] How to get value of promise that already resolved?
问题描述
我有我在我的UI结合的承诺。当承诺解决和UI元素渲染然后我就可以点击UI元素。在我的控制器code,其中我处理click我想采取行动的承诺的价值。在我的角度code这一点,我已经知道,承诺解决..但是,当我想要得到的值,其犹一个承诺。
I have a promise that I am binding to in my UI. When the promise resolves and the UI element renders I can then click on that UI element. In my controller code where I handle the click I would like to act on the value of the promise. At this point in my angular code I already know that the promise is resolved.. but when I want to get the value its still a promise.
请告诉我最好的方式得到承诺的价值......因为我知道它一定已经解决了?
Whats the best way to get the value of the promise... given that I know it must have resolved?
推荐答案
承诺始终是承诺 - 这是他们应该的。虽然是可能的一个承诺直接绑定到视图,我一般因为它缺少透明度,并可能导致视图闪烁阻止这种做法。更好的方法是在指定的范围值然后
电话:
Promises are always promises - and that's how they should be. While it is possible to bind a promise directly to the view, I generally discourage this practice because it lacks transparency and can lead to view flickering. A better solution is to assign a scope value in a then
call:
myService.then(function( val ) {
$scope.val = val;
});
在 $ scope.val
准备好了,它总是可以直接视为价值 - 因为它是
When $scope.val
is ready, it can always be treated directly as a value - because it is.
这是说,如果你想直接做的承诺任务,你就必须把它当作一个承诺;也就是说,你需要调用 promise.then(函数(){...});
。这似乎是一个不便之处,但它实际上是证明了强大的承诺是多么的。
That said, if you want to do the promise assignment directly, you just have to treat it like a promise; that is, you need to call promise.then(function () { ... });
. This seems like an inconvenience, but it's actually a testament to just how powerful promises are.
如果你想在承诺一些更多的信息,我pontificated他们(并提供了code的例子)对于最近Google+视频群聊,在这里我介绍的优势,常见的用途,并在AngularJS承诺的最佳做法:<一HREF =https://plus.google.com/events/cljavmi7kpup1fso43k3fkpk2eg> AngularJS小聚 - 承诺,承诺
公司承诺,承诺。永无止境的承诺链的想法是专门处理,但我没有时间code从我的头顶。
If you want some more info on promises, I pontificated on them (and provided code examples) for a recent Google+ Hangout, where I covered the advantages, common uses, and best practices of promises in AngularJS: AngularJS Hangout -- Promises, Promises Promises, Promises. The idea of never-ending promise chains was specifically addressed, though I don't have a timecode off the top of my head.
希望这有助于!
这篇关于如何获得承诺的价值已经解决了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!