1.4.1角UI路由器10 $摘要()的迭代时呼吁$ stateChangeStart $ state.go [英] Angular 1.4.1 UI Router 10 $digest() iterations when $state.go called on $stateChangeStart

查看:228
本文介绍了1.4.1角UI路由器10 $摘要()的迭代时呼吁$ stateChangeStart $ state.go的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有需要授权的状态。我听 $ stateChangeStart 事件,如果 toState.data.protected 键,用户没有授权我称之为即preventDefault() $ state.go('登录')

I have a state that requires authorization. I listen to the $stateChangeStart event and if the toState.data.protectedand the user is not authorized I call e.preventDefault() and $state.go('login').

当我打开根URL应用程序,我自动重定向到保护状态。
这将导致10 $消化循环和我结束了在登录状态下,当我打开根URL的应用程序,我自动重定向到保护状态。

When I open the app in root url I'm automatically redirected to protected state. This causes 10 $digest loops and I end up in the login state when I open the app in the root url and I'm automatically redirected to a protected state.

未捕获的错误:[$ rootScope:infdig] 10 $摘要()迭代到达。中止!

看到这个plnkr: http://plnkr.co/edit/1voh7m?p = preVIEW

See this plnkr: http://plnkr.co/edit/1voh7m?p=preview

我成功地使用类似code在不同的项目中具有角1.2.26没有任何错误。

I successfully use similar code in different project with angular 1.2.26 with no errors.

举例code 角1.4.1,ui.router 0.2.15

//config block
$urlRouterProvider.otherwise('/main');   
$stateProvider
 .state('main', {
   url: '/main',
     templateUrl: 'main.html',
     controller: 'MainController as main',
     data: {'protected': true}
 }) 
 .state('login', {
     url: '/login',
     templateUrl: 'login.html',
     controller: 'LoginController as login'
 });

// in a run block
$rootScope.$on("$stateChangeStart", function (event, toState) {
    if (!event.defaultPrevented && toState.data &&
            toState.data.protected) {
        // the user is not authorized, do not switch to state
        event.preventDefault();
        // go to login page
        $state.go('login');
    }
});

你知道是什么原因造成的循环?

Do you know what causes the loop?

我不知道的事情可能发生这样的:

I wonder if the things might be happening like this:


  1. 拦截过渡到main.submain状态

  2. 开始过渡到登录状态

  3. UI路由器获取信息,第一transiotion被取消了

  4. UI路由器运行 $ urlRouter.update()键,开始过渡到main.submain

  1. Intercept the transition to main.submain state
  2. Start transition to login state
  3. UI router gets the information that the first transiotion got cancelled
  4. UI router runs $urlRouter.update() and starts transition to main.submain

编辑:简化的状态配置

推荐答案

这是UI.Router的问题 - 在Github上查看此问题:的 https://github.com/angular-ui/ui-router/issues/600

This is an issue of the UI.Router – see this issue on Github: https://github.com/angular-ui/ui-router/issues/600

基本上,如果你使用。否则('/主')(也指出了@Grundy),那么该网址更改为 /主时,路径不能得到解决。后 $ locationChangeSuccess 我的监听器被调用,我拒绝使用事件。preventDefault重定向()。这导致位置改回未知路径从而使后备路径被再次使用。这会导致无限循环。解决的办法是这样的:

Basically, if you use .otherwise('/main') (also pointed out by @Grundy) then the url is changed to /main when the path cannot be resolved. After $locationChangeSuccess my listener is called and I reject the redirection using event.preventDefault(). This causes the location to change back to the unknown path thus causing the fallback path to be used again. This causes the infinite loop. The solution is this:

$urlRouterProvider.otherwise(function($injector) {
  var $state = $injector.get('$state');
  $state.go('main');
});

您可以说出一个都会调用与 $注射器功能,你可以无背的往复位置变化重定向到您的主状态(或404)。 THX到Github上的球员,我应该张贴这个问题之前搜查那里。

You can state a function which gets called with $injector and you can redirect to your main state (or 404) without back-and-forth location changes. Thx to the guys on Github, I should have searched there before posting this question.

工作普拉克: http://plnkr.co/edit/eQXaIk

这篇关于1.4.1角UI路由器10 $摘要()的迭代时呼吁$ stateChangeStart $ state.go的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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