角ui路由器中的承诺依赖解决顺序 [英] Promise dependency resolution order in angular ui-router

查看:118
本文介绍了角ui路由器中的承诺依赖解决顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了一个顶级的控制器,只有当一个承诺(由 Config factory返回)成功解决时才被实例化。该承诺基本上是通过RESTful端点下载Web应用程序配置,等等。

I have set up a top-level controller that is instantiated only when a promise (returned by a Config factory) is successfully resolved. That promise basically downloads the Web app configuration, with RESTful endpoints and so on.

$stateProvider
  .state('app', {
    url: '/',
    templateUrl: 'views/_index.html',
    controller: 'MainCtrl',
    resolve: {
      config: 'Config'
    }
  });

此设置允许我断言,在任何下级控制器获取之前,配置正确加载有机会使用它。

This setup allows me to kind-of assert that the configuration is properly loaded before any lower controller gets a chance to use it.

现在我需要在一个更深入的嵌套控制器中注入另一个工厂 code> Config ,仅在解决时才起作用(看起来像一个需要一些Web服务URL的 $ resource 包装器)。如果我这样做:

Now I need to inject, in a deeper nested controller, another factory that uses Config and only works when it is resolved (look at it like a $resource wrapper that needs some Web service URLs). If I do:

$stateProvider
  .state('app.bottom.page', {
    url: '/bottom/page',
    templateUrl: 'views/_a_view.html',
    controller: 'BottomLevelCtrl',
    resolve: {
      TheResource: 'MyConfigDependingResource'
    }
  });

它看起来像解决评估订单因此,不要从上到下按照控制器层次结构,而是从下到上依次:

it looks like the resolve evaluation order does not follow the controller hierarchy from top to bottom, but from bottom to top, therefore:


  1. app.bottom。 输入

  2. ui-router 尝试解析 MyConfigDependingResource ,但注入失败,
    ,因为 Config 从未被初始化

  3. ui-router 分辨率因为错误而停止(甚至没有抛出错误,但这是另一个问题),而配置从来没有由顶层控制器初始化

  1. app.bottom.page is entered
  2. ui-router attempts to resolve MyConfigDependingResource, but the injection fails, because Config has never been initialized
  3. The ui-router resolution stops because of an error (without even throwing Errors, but that's another issue), and Config is never initialized by the top level controller

为什么 ui-router 以相反的顺序解析依赖关系?如何轻松地解决我的 TheResource 对象之后顶层 MainCtrl 已解决配置(不依赖 $ inject ,当然)?

Why is ui-router resolving dependencies in a reverse order? How can I easily resolve my TheResource object after the top level MainCtrl has resolved Config (without relying on $inject, of course)?

更新:从此plnkr的日志,您可以看到只有在嵌套控制器启动自己的解析过程之后,才尝试顶级解析

UPDATE: from this plnkr's log you can see that the top level resolve is attempted only after the nested controller has started its own resolving process.

推荐答案

与@Kasper Lewau的答案类似,可以使用单个状态指定对解析的依赖。如果您的一个解析依赖于同一个解析块中的一个或多个解析属性。在我的情况下, checkS 依赖于另外两个解析

Similarly to @Kasper Lewau's answer, one may specify a dependency on resolves withing a single state. If one of your resolves depends on one or more resolve properties from the same resolve block. In my case checkS relies on two other resolves

.state('stateofstate', {
    url: "/anrapasd",
    templateUrl: "views/anrapasd.html",
    controller: 'SteofsteCtrl',
    resolve: {
        currU: function(gamMag) {
            return gamMag.checkWifi("jabadabadu")
        },
        userC: function(gamUser, $stateParams) {
            return gamUser.getBipi("oink")
        },
        checkS: ['currU', 'userC', 'gamMag', function(currU, userC, gamMag) {
            return gamMag.check(currU, userC);
        }]
    }
})






** PS:**检查的解决部分以下文档有关解析的内部工作的更多详细信息。


**PS: **Check the "Resolves" section of the following document for more details about the inner-workings of resolve.

这篇关于角ui路由器中的承诺依赖解决顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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