在AngularUI路由器,做悬而未决的承诺泄漏? [英] In AngularUI Router, do unresolved promise leak?

查看:114
本文介绍了在AngularUI路由器,做悬而未决的承诺泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我采用了棱角分明的UI路由器,我想知道是否有人使用解析会泄漏时,知道与否悬而未决的承诺。我们的使用情况是,在某些国家,我们需要做一些检查,然后将原来的状态加载之前过渡到一个不同的URL。

I am using angular UI router and I was wondering if anyone knew whether or not unresolved promises when using resolve would leak. Our use case is that in certain states, we needed to do some checks and then transition to a different url before the original state loads.

我们处理了这个正在做检查,并使用 $位置解决交换链接的方式和退出悬而未决的承诺。悬而未决的承诺是用来prevent原始状态的控制器和模板可供加载(否则他们会一直引发的错误)。

The way we handled this was doing the check and switching the url using $location inside resolve and leaving an unresolved promise. The unresolved promise was used to prevent the original state's controllers and templates from loading (or else they would've thrown errors).

所以我的问题是,不留下悬而未决的承诺,这种做法会导致泄漏?我知道另一种选择是设置一个长 $超时解决的承诺,但如果没有必要,我想避免它。

So my question is, does this practice of leaving unresolved promises cause leakage? I realize an alternative option is to set a long $timeout for resolving the promises but if its not necessary, I would like to avoid it.

推荐答案

您将需要解决或拒绝承诺。我建议的URL转换将采取在 $ stateChangeError 事件侦听器,这将被拒绝承诺被解雇的地方。您可以通过位置,您想要去的拒绝([数据])来监听器。

You would need to resolve or reject the promise. I would suggest that the URL switching would take place in the $stateChangeError event listener, which would be fired by rejection of the promise. You can pass the location you want to go to in the reject([data]) to the listener.

http://fiddle.jshell.net/L9jxf/2/

有些承诺在超时后,将拒绝(模拟服务器调用)

Some promise that will reject after a timeout (simulates server call)

        protected: ['$timeout', '$q', function ($timeout, $q) {
            var deferred = $q.defer();
            $timeout(function () {
                deferred.reject({type:'redirect',location:'401'});
            }, 1000);
            return deferred.promise;
        }]

此处理拒绝

app.run(function ($rootScope, $state) {
    $rootScope.$on('$stateChangeError', function (e, to, toParams, from, fromParams, error) {
        if (error.type === 'redirect') {
            $state.transitionTo(error.location);
        }
    });
});

这篇关于在AngularUI路由器,做悬而未决的承诺泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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