在UI的路由器的$ stateChangeStart无限循环 [英] Infinite Loop on ui-router's $stateChangeStart

查看:280
本文介绍了在UI的路由器的$ stateChangeStart无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大规模应用的 UI路由器

和使用重定向到一个不同的状态,如果一个precondition不满足挣扎:

And struggling with redirecting to a different state if a precondition is not met:

我试图用一个拦截器:(<一href=\"http://stackoverflow.com/questions/22992724/how-do-i-$p$pform-a-redirect-in-an-angular-interceptor\">How我做preform在角拦截重定向)。

I tried using an interceptor: (How do I preform a redirect in an angular interceptor).

不过,有人提到在处理$ stateChangeState会更合适。但我仍然运行到一个无限循环:

But someone mentioned that handling $stateChangeState would be more appropriate. But I am still running into an infinite loop:

    /**
     *  Check here for preconditions of state transitions
     */
    $rootScope.$on('$stateChangeStart', function(event, toState) {

        // which states in accounts to be selected
        var accountRequiredStates = ['user.list', 'user.new'];
        if(_.contains(accountRequiredStates, toState.name)){
            event.preventDefault();
            ApiAccount.customGET('get_current').then(function(resp){
                // if I have a selected account, go about your business
                if(resp.hasOwnProperty('id')){
                    $state.go(toState.name);
                } else { // prompt user to select account
                    $state.go('user.select_account');
                }
            })
        }
    });

任何人都可以提出一个更好的模式(一个工程)

Can anyone suggest a better pattern (one that works)

谢谢!

请注意:类似的问题,不同的方法在这里:<一href=\"http://stackoverflow.com/questions/22992724/how-do-i-$p$pform-a-redirect-in-an-angular-interceptor\">How我做preform在角拦截器重定向

Note: Similar problem different approach here: How do I preform a redirect in an angular interceptor

推荐答案

我不认为有什么不对你要做到这一点,虽然我不是专家的一般方式。我看到其中看起来像它可能会导致一个无限循环的执行缺陷。比方说,用户尝试去'user.new状态。您$ stateChangeStart监听截获,取消它,你的customGET;那么如果如果条件为真内( resp.hasOwnProperty('身份证')),尝试将用户发送到相同的user.new状态。在这一点,你的$ stateChangeStart监听截获它,它取消等,一遍又一遍。

I don't think there's anything wrong with the general way you're trying to do this, though I'm not an expert. I do see a flaw in the implementation which looks like it could cause an infinite loop. Let's say the user tries to go to 'user.new' state. Your $stateChangeStart listener intercepts that, cancels it, does your customGET; then if the inner if condition is true (resp.hasOwnProperty('id')), you try to send the user to the same 'user.new' state. At which point, your $stateChangeStart listener intercepts it, cancels it, etc., over and over.

我避免在code这个问题的办法是有一个变量(在我宣布侦听服务)帮我绕过检查:
VAR middleOfRedirecting = FALSE; 在你的内心,如果 resp.hasOwnProperty('身份证')查询,设置middleOfRedirecting内块为true;在您$ stateChangeStart监听开始时只调用事件。preventDefault()添加条件和重定向如果middleOfRedirecting是假的。您还需要一个$ stateChangeSuccess监听器设置middleOfRedirecting回false,重置它的下一个状态变化。 (我觉得应该有比这更好的方式,但它至少起作用。)

The way I avoid this problem in my code is to have a variable (in the service where I declare the listener) to help me bypass that check: var middleOfRedirecting = false; Inside your inner if block within the resp.hasOwnProperty('id') check, set middleOfRedirecting to true; add a condition at the start of your $stateChangeStart listener to only call event.preventDefault() and redirect if middleOfRedirecting is false. You also would need a $stateChangeSuccess listener to set middleOfRedirecting back to false, resetting it for the next state change. (I feel like there should be a better way than this, but it at least works.)

这篇关于在UI的路由器的$ stateChangeStart无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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