角JS UI路由器如何重定向从父项的子状态? [英] Angular JS ui-router how to redirect to a child state from a parent?

查看:144
本文介绍了角JS UI路由器如何重定向从父项的子状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用的OnEnter重定向到一个国家,如果新状态是当前状态的孩子,会出现一个无限循环。

示例:

  $ stateProvider
  .STATE'库存',
    网址:'/库存
    templateUrl:意见/ inventory.html
    控制器:'InventoryCtrl
    的OnEnter:() - >
      $ state.goinventory.low
  .STATEinventory.low',
    网址:'/低
    templateUrl:意见/库存table.html
    控制器:'LowInventoryCtrl

  $ state.go'inventory.low

被调用时,状态广告资源重新初始化,使其再次调用=无限循环。

然而,如果重定向状态是:

  $ state.go'otherStateThatIsNotAChild

此问题不会发生。我假设父状态正在重新初始化,但为什么呢?


  1. 为什么父状态下,当。去被称为对孩子的状态?
  2. 重新初始化
  3. 如何那么,你会处理重定向到一个孩子的状态?


解决方案

  

1)为什么。去的时候上调用正在重新初始化父状态
  孩子的状态?


虽然过渡是在过程中,任何$ state.go / transitionTo将导致的目前在过程的过渡也可以被取代。被取代的过程中的过渡立即取消。由于原来的过渡到广告资源不是所有州的onEnters被调用,原来的过渡被取消,一个的新的的过渡时间完成 inventory.low 从previously活动状态。

启动

请参阅UI路由器SRC <一个href=\"https://github.com/angular-ui/ui-router/blob/master/src/state.js#L897\">https://github.com/angular-ui/ui-router/blob/master/src/state.js#L897 ...


  

2)如何那么,你会处理重定向到一个孩子的状态?


您可以......


  • $ state.go $超时()允许重定向之前的原始过渡来完成。

  • 呼叫$ state.go从您的控制器来代替。后过渡完成UI路由器调用从用户界面视图指令的控制器。

在任何情况下,非常肯定你希望你的应用程序,以这样的重定向。如果用户直接导航到库存的任何其他子状态(如 inventory.high ),还是可能导致重定向,迫使他们库存。低这不会是他们想什么。

When using onEnter to redirect to a state, if the new state is a child of the current state, an infinite loop occurs.

Example:

$stateProvider
  .state 'inventory',
    url: '/inventory'
    templateUrl: 'views/inventory.html'
    controller: 'InventoryCtrl'
    onEnter: () ->
      $state.go 'inventory.low'
  .state 'inventory.low',
    url: '/low'
    templateUrl: 'views/inventory-table.html'
    controller: 'LowInventoryCtrl'

When:

$state.go 'inventory.low'

Is called, the state inventory is re-initialized, causing it to be called again = infinite loop.

However, if the redirect state is:

$state.go 'otherStateThatIsNotAChild'

This issue does not occur. I assume that the parent state is being re-initialized, but why?

  1. Why is the parent state being reinitialized when .go is called on a child state?
  2. How then, would you handle redirecting to a child state?

解决方案

1) Why is the parent state being reinitialized when .go is called on a child state?

While a transition is in process, any $state.go/transitionTo will cause the currently in process transition to be Superseded. An in-process transition that is superseded is cancelled immediately. Since your original transition to inventory is not completed by the time all the states' onEnters are called, the original transition is cancelled and a new transition to inventory.low is started from the previously active state.

See ui-router src https://github.com/angular-ui/ui-router/blob/master/src/state.js#L897 ...

2) How then, would you handle redirecting to a child state?

You could...

  • Wrap $state.go in a $timeout() to allow the original transition to complete before redirecting.
  • Call $state.go from your controller instead. UI-router invokes the controller from the ui-view directive AFTER the transition is completed.

In any case, be very sure you want your app to redirect like this. If a user navigated directly to any other child state of inventory (such as inventory.high), the redirect will still occur, forcing them to inventory.low which would not be what they intended.

这篇关于角JS UI路由器如何重定向从父项的子状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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