试图使Angularjs显示的承诺时,无限循环 [英] Infinite loop when trying to make Angularjs display a promise

查看:110
本文介绍了试图使Angularjs显示的承诺时,无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

努力使AngularJS显示的承诺,在这篇文章中解释说,当我面临着一个无限循环:的 http://markdalgleish.com/2013/06/using-promises-in-angularjs-views/

我第一个电话 $ parseProvider.unwrapPromises(真);

的.config([$ parseProvider
功能($ parseProvider){
    $ parseProvider.unwrapPromises(真);
}])

下面是我厂用来访问的消息:

app.factory(MessageHelper,[
功能(){    //为简洁起见省略:
    //调用服务器获取消息    返回{        得到:函数(code){
            变参= Array.prototype.slice.call(参数);
            args.shift();
            如果(消息){
                返回$ q.when(格式(消息[code],参数));
            }
            变种推迟= $ q.defer();
            deferredMessages.push({code:code,ARGS:ARGS,延期:推迟});
            返回deferred.promise;
        }    };}]);

然后,在控制

$ scope.msg = {
    得到:函数(){
        返回MessageHelper.get.apply(这一点,参数);
    }
};

最后,在视图中:

<跨度> {{msg.get(existing.message code,第一个参数 第二个参数)}}< / SPAN>

这里是无限循环: $ scope.msg.get 被称为无限...任何想法,为什么


解决方案

您不能以任何角度的前pressions一个副作用范围任何东西,否则无限循环的意志发生了。

这是由设计,直到一切都没有改变反复重新运行所有的前pressions。所以当时的承诺得到解决,前pressions将得到再次评估。<​​/ P>

要避免这种情况,你可以移动 msg.get 调入控制器和承诺作为另一个属性存储在 $范围

  $ scope.msg = {
    得到:函数(){
        返回MessageHelper.get.apply(这一点,参数);
    }
};$ scope.resultMsg = $ scope.msg.get(existing.message code,第一个参数,第二参数);

和使用模板来代替:

  {{resultMsg}}

希望这有助于。

I'm facing an infinite loop when trying to make AngularJS display a promise, as explained in this article: http://markdalgleish.com/2013/06/using-promises-in-angularjs-views/.

I first call $parseProvider.unwrapPromises(true);:

.config(["$parseProvider",
function($parseProvider) {
    $parseProvider.unwrapPromises(true);
}])

Here is the factory I use to access a message:

app.factory("MessageHelper", [
function() {

    // omitted for brevity:
    // calling the server for getting the messages

    return {

        get: function(code) {
            var args = Array.prototype.slice.call(arguments);
            args.shift();
            if (messages) {
                return $q.when(format(messages[code], args));
            }
            var deferred = $q.defer();
            deferredMessages.push({ code: code, args: args, deferred: deferred });
            return deferred.promise;
        }

    };

}]);

Then, in a controller:

$scope.msg = {
    get: function() {
        return MessageHelper.get.apply(this, arguments);
    }
};

Finally, in a view:

<span>{{msg.get("existing.message.code", "first param", "second param")}}</span>

And here is the infinite loop: $scope.msg.get gets called infinitely... Any idea why?

解决方案

You can't have anything with a side effect to scope in any angular expressions, otherwise the infinite loop will occurred.

It's by design to rerun all the expressions repeatedly until nothing has changed. So at the time the promise is resolved, the expressions will get evaluated again.

To avoid that, you could move the msg.get call into the controller and store the promise as an another property in $scope:

$scope.msg = {
    get: function() {
        return MessageHelper.get.apply(this, arguments);
    }
};

$scope.resultMsg = $scope.msg.get("existing.message.code", "first param", "second param");

and use that in the template instead:

{{resultMsg}}

Hope this helps.

这篇关于试图使Angularjs显示的承诺时,无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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