角 - 动态地选择初始状态 [英] Angular - Dynamically Choose Starting State

查看:100
本文介绍了角 - 动态地选择初始状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序会自动启动在默认状态(即具有URL:/),很明显。让我们把它叫做状态 A

My app automatically starts at the default state (that has url: "/"), obviously. Let's call it state A.

我要忍住就在某一个条件是truthy情况。这意味着,如果 X === 4 例如然后我想状态 B 是第一个负载。

I want to hold back on that in case that a certain condition is truthy. Meaning that if x === 4 for instance then I want state B to be the first one that loads.

我的问题是,我检查的情况,在时间上 app.run 例如,默认状态已经被加载并$ P $在psented视图,然后才切换到状态 B

My problem is that by the time I check for that condition, in the app.run for instance, the default state is already being loaded and presented in the view, and only then switches to state B.

有什么办法,我可以忍住状态加载和系统kickstart只后,我检查车况?

Is there any way I can hold back the state loading and kickstart it only after I check the condition ?

推荐答案

一个工作示例

作为注释讨论,我们可以使用本机,内置的功能,与UI的路由器来。其中之一是<一个href=\"http://%20angular-ui.github.io/ui-router/site/#/api/ui.router.router.$urlRouterProvider#methods_deferintercept\"相对=nofollow> $ urlRouterProvider.deferIntercept(延迟)

As discussed in comments we can use the native, built-in features, coming with UI-Router. One of them is $urlRouterProvider.deferIntercept(defer)

禁用或有助延缓位置改变拦截。

Disables (or enables) deferring location change interception.

如果你想自定义同步URL的行为(例如,如果你想推迟一个过渡,但保持当前的URL),调用在配置时此方法。然后,在运行时,调用 $ urlRouter.listen()您已经配置了自己的 $ locationChangeSuccess 事件处理程序之后。

If you wish to customize the behavior of syncing the URL (for example, if you wish to defer a transition but maintain the current URL), call this method at configuration time. Then, at run time, call $urlRouter.listen() after you have configured your own $locationChangeSuccess event handler.

我。步骤 - 停止执行

I. step - STOP execution

所以,我们可以定义状态 的.config()

So, we can define states in .config() phase

.config(['$stateProvider', '$urlRouterProvider','$locationProvider',
    function ($stateProvider, $urlRouterProvider,$locationProvider) {

        // States
        $stateProvider
          ...
          .state('parent', {
              ...
          })
          .state('parent.child', { 
              ...
          })
          ;

        ...
        // here we say STOP
        $urlRouterProvider.deferIntercept();
    }
])

二。步骤 - 初始化认为需要在.RUN() - 重新启用执行

II. step - init what needed in .run() - re-enable execution

这可能是运行一些简单的例子:

This could be some naive example of run:

.run(['$urlRouter', '$timeout', '$state',
  function($urlRouter, $timeout, $state) {

    $timeout(function(){

      // here we turn all on again...
      $urlRouter.sync();
      $urlRouter.listen();

      // there could be some decision, driven by $http load
      // we just use some state as default target
      $state.go("parent.child");
    }, 1000)

}]);

硒都等待一秒钟,然后我们就重新启用所有的执行和重定向到parent.childd......但可能是任何其他根据 .RUN()

Se do wait for a second and then we do re-enable all the execution and redirect to "parent.childd" ... but could be any other, based on some decision done in .run() phase

检查一下这里

这篇关于角 - 动态地选择初始状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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