angularJS中装饰函数的依赖注入

angularJS中装饰函数的依赖注入,angularjs,dependency-injection,decorator,angular-decorator,Angularjs,Dependency Injection,Decorator,Angular Decorator,使用angularJS时,您可以使用$provide.decorator('thatService',decoratorFn)为服务注册装饰函数 创建服务实例后,$injector将把它(服务实例)传递给注册的装饰函数,并将该函数的结果用作装饰服务 现在假设thatService使用它注入的thatOtherService 我如何才能获得对thatOtherService的引用,以便我能够在我的装饰师想要添加到thatService的.myNewMethodForThatService()中使用

使用angularJS时,您可以使用
$provide.decorator('thatService',decoratorFn)
为服务注册装饰函数

创建服务实例后,
$injector
将把它(服务实例)传递给注册的装饰函数,并将该函数的结果用作装饰服务

现在假设
thatService
使用它注入的
thatOtherService


我如何才能获得对
thatOtherService
的引用,以便我能够在我的装饰师想要添加到
thatService
.myNewMethodForThatService()
中使用它?

这取决于具体的用例-需要更多信息才能得到明确的答案。
(除非我误解了要求)这里有两种选择:

1) 从
ThatService
公开
ThatOtherService

.service('ThatService', function ThatServiceService($log, ThatOtherService) {
  this._somethingElseDoer = ThatOtherService;
  this.doSomething = function doSomething() {
    $log.log('[SERVICE-1]: Doing something first...');
    ThatOtherService.doSomethingElse();
  };
})
.config(function configProvide($provide) {
  $provide.decorator('ThatService', function decorateThatService($delegate, $log) {
    // Let's add a new method to `ThatService`
    $delegate.doSomethingNew = function doSomethingNew() {
      $log.log('[SERVICE-1]: Let\'s try something new...');

      // We still need to do something else afterwards, so let's use
      // `ThatService`'s dependency (which is exposed as `_somethingElseDoer`)
      $delegate._somethingElseDoer.doSomethingElse();
    };

    return $delegate;
  });
});
2) 在decorator函数中插入
ThatOtherService

.service('ThatService', function ThatServiceService($log, ThatOtherService) {
  this.doSomething = function doSomething() {
    $log.log('[SERVICE-1]: Doing something first...');
    ThatOtherService.doSomethingElse();
  };
})
.config(function configProvide($provide) {
  $provide.decorator('ThatService', function decorateThatService($delegate, $log, ThatOtherService) {
    // Let's add a new method to `ThatService`
    $delegate.doSomethingNew = function doSomethingNew() {
      $log.log('[SERVICE-2]: Let\'s try something new...');

      // We still need to do something else afterwatds, so let's use
      // the injected `ThatOtherService`
      ThatOtherService.doSomethingElse();
    };

    return $delegate;
  });
});


您可以在本文中看到这两种方法都在起作用

我的印象是,第二种选择是不可能的,因为服务在配置阶段还不可用。但是中的decorator没有在配置阶段运行(它只是注册的)。它实际上是在“运行”阶段运行的,所以服务可以像往常一样进行注入。感谢您的澄清。