AngularJS - 绑定/观察返回promise的函数

时间:2022-05-01 19:37:19

I posted an issue on the AngularJS github but it doesn't seem to be getting a whole lot of attention and I wasn't able to fix it myself since it's a pretty low-level issue, so I think it's time to look for a workaround.

我在AngularJS github上发布了一个问题,但它似乎没有得到很多关注,我自己也无法解决它,因为它是一个非常低级别的问题,所以我认为是时候寻找一个解决方法。

Angular allows you to put a promise (or anything with a .then(...) function) into your scope, and once it is resolved, all $watches and anything bound to that promise will use the resolved value. The issue arises when you use a function to return a promise, as the same doesn't apply - it's handled like a plain object.

Angular允许您将promise(或带有.then(...)函数的任何东西)放入您的范围,一旦解析,所有$ watches和绑定到该promise的任何内容都将使用已解析的值。当您使用函数返回promise时会出现问题,因为它不适用 - 它像普通对象一样处理。

For example:

例如:

var helloDef = $.Deferred();
$scope.hello = helloDef.promise();

$scope.getHello = function() {
    return $scope.hello;
};

$timeout(function() {
    helloDef.resolve('Hello!');
}, 1000);

Fiddle

小提琴

Here using ng-bind="hello" works fine and outputs Hello!, but ng-bind="getHello()" outputs [object Object] as the internal $watch returns the promise object. Works the same way with $q instead of $.Deferred.

这里使用ng-bind =“hello”工作正常并输出Hello!,但ng-bind =“getHello()”输出[object Object],因为内部$ watch返回promise对象。使用$ q而不是$ .Deferred工作方式相同。

In my actual code I'm creating the promise the first time the function is called, so I can't just pre-make the promise and refer to that in scope.

在我的实际代码中,我在第一次调用函数时创建了promise,所以我不能只是预先做出承诺并在范围内引用它。

I also need it for more than just ng-bind, so making my own binding directive which handles this case properly isn't viable.

我还需要它不仅仅是ng-bind,所以制作我自己的绑定指令来正确处理这种情况是不可行的。

Anyone have any ideas? I'm currently returning the promise if the data hasn't resolved and the actual result if it has, but that's a pain to work with. Anything bound to the data briefly causes weird side effects as the data is being loaded, like ngRepeat using the promise object instead of the resolved value to create elements.

有人有主意吗?我现在正在返回承诺,如果数据没有解决,实际结果如果有,但这是一个痛苦的工作。与数据绑定的任何内容都会在加载数据时短暂地产生奇怪的副作用,例如使用promise对象而不是使用已解析的值创建元素的ngRepeat。

Thanks.

谢谢。

UPDATE: Pull request: https://github.com/angular/angular.js/pull/3605

更新:请求:https://github.com/angular/angular.js/pull/3605

UPDATE 2: For future reference, automatic promise unwrapping has been deprecated in the 1.2 brach.

更新2:为了将来参考,自动承诺展开已在1.2 brach中弃用。

2 个解决方案

#1


1  

For some things, I use $resource. If I need to wait for it, $then works well:

对于某些事情,我使用$ resource。如果我需要等待它,$然后效果很好:

var r = $resource...get...
var p = r.$then...

Otherwise, I build my own resource-like object that is not a promise, but has a promise that I can wait for.

否则,我构建自己的资源类对象,这不是一个承诺,但有一个我可以等待的承诺。

#2


0  

Pull request merged, it should be fixed in 1.2.0 so I'll mark this as the answer.

Pull请求合并,应该在1.2.0中修复,所以我将此标记为答案。

#1


1  

For some things, I use $resource. If I need to wait for it, $then works well:

对于某些事情,我使用$ resource。如果我需要等待它,$然后效果很好:

var r = $resource...get...
var p = r.$then...

Otherwise, I build my own resource-like object that is not a promise, but has a promise that I can wait for.

否则,我构建自己的资源类对象,这不是一个承诺,但有一个我可以等待的承诺。

#2


0  

Pull request merged, it should be fixed in 1.2.0 so I'll mark this as the answer.

Pull请求合并,应该在1.2.0中修复,所以我将此标记为答案。