棱角分明的UI路由器 - 名为依赖遗传解析的依赖 [英] angular-ui router -- inherited resolved dependencies for NAMED dependencies

查看:187
本文介绍了棱角分明的UI路由器 - 名为依赖遗传解析的依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每文档中的例子,孩子的状态将继承解决从父状态的依赖。此外,还可以对父母的依赖许诺孩子被注入到键子状态初始化之前得到解决。

Per the example in the documentation, child states will inherit resolved dependencies from parent states. Furthermore, you can have promises for parent dependencies be resolved before children are instantiated by injecting keys into child states.

请参阅例如,从文档:

$stateProvider.state('parent', {
      resolve:{
         resA:  function(){
            return {'value': 'A'};
         }
      },
      controller: function($scope, resA){
          $scope.resA = resA.value;
      }
   })
   .state('parent.child', {
      resolve:{
         resB: function(resA){
            return {'value': resA.value + 'B'};
         }
      },
      controller: function($scope, resA, resB){
          $scope.resA2 = resA.value;
          $scope.resB = resB.value;
      }

不过,你怎么做到这一点,如果依赖而得名,不是一个函数。例如,看到粗体部分:

However, how do you do this if the dependency is NAMED, not a function. For example, see bolded part:

$stateProvider.state('parent', {
      resolve:{
         resA:  'ServiceA'
         }
      },
      controller: function($scope, ServiceA){
          $scope.ServiceA = ServiceA.value;
      }
   })
   .state('parent.child', {
      resolve:{
         ServiceB: ServiceB
         }
      },
      controller: function($scope, ServiceA, ServiceB){
      }

我无法弄清楚如何使ServiceB等待ServiceA先实例化之前进行实例化。

I can't figure out how to make ServiceB wait for ServiceA to first be instantiated before instantiating.

我试图把ServiceA作为ServiceB的依赖,但不起作用。

I tried putting 'ServiceA' as a dependency for ServiceB, but that doesn't work.

感谢您事先的任何帮助。

Thanks in advance for any help.

推荐答案

这无关与UI的路由器。你只是想知道的是你如何先于另一个实例化一个服务。答案是,你不能,需要改变你的设计。

This has nothing to do with ui-router. What you simply want to know is how do you instantiate one service before another. The answer is that you can't, and need to change your design.

如果里面有需要完成,使 ServiceB 可以使用它ServiceA 的东西,那么你应该使用 ServiceA内承诺。例如:

If there is something inside ServiceA that needs to complete so that ServiceB can consume it, then you should use promises inside ServiceA. For example:

.factory('ServiceA', function($q){
    var deferred = $q.defer();       

    // Do some kind of work here and when complete, run deferred.resolve();

    return {
        myPromise: function() { return deferred.promise; }
    };
});

然后在消耗 ServiceB

.factory('ServiceB', function(ServiceA){
    ServiceA.myPromise().then(function(){
        // This will run after your ServiceA work has completed
    });
});

了解更多信息的 $ Q 文档:的链接这里

这篇关于棱角分明的UI路由器 - 名为依赖遗传解析的依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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