无极角度UI路由器的依赖解析顺序 [英] Promise dependency resolution order in angular ui-router

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

问题描述

我已经建立了一个只实例化时的承诺(以配置工厂返回)成功解决了顶级控制器。这一承诺基本下载网络应用配置,REST风格的端点等。

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.

现在我需要注入,在更深的嵌套控制器,另一个工厂使用配置键,只能当它解决了(看它像一个 $资源,它需要一些Web服务网址包装)。如果我做的:

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.page 输入

  2. UI路由器尝试解析 MyConfigDependingResource ,但注射失败,
    因为配置从未初始化

  3. UI路由器的决议停止,因为一个错误的(不连抛错误 S,但这是另一个问题)和配置永远不会被顶级控制器初始化

  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路由器解决相反的顺序依赖呢?我怎么能轻易解决我的 TheResource 对象的 顶级 MainCtrl 已解决后,配置(不依赖于 $注,当然)?

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的回答,可以指定在结算时withing一个国家的依赖。如果做出决议的一个取决于从同样的决心块的一个或多个解析性能。在我的情况检查依赖于另外两个议决

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:**检查的Resolves决定部分下面的文档有关决心

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

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