angularJS 中装饰器函数的依赖注入
dependency injection into decorator functions in angularJS
使用 angularJS 时,您可以使用 $provide.decorator('thatService',decoratorFn)
为服务注册装饰函数。
创建服务实例后,$injector
会将其(服务实例)传递给已注册的装饰函数,并将该函数的结果用作装饰服务。
现在假设 thatService
使用已注入其中的 thatOtherService
。
我如何获得对 thatOtherService
的引用,以便我可以在我的 decoratorFN 想要添加到 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)在装饰器函数中注入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;
});
});
您可以在此 demo 中看到这两种方法的实际应用。
使用 angularJS 时,您可以使用 $provide.decorator('thatService',decoratorFn)
为服务注册装饰函数。
创建服务实例后,$injector
会将其(服务实例)传递给已注册的装饰函数,并将该函数的结果用作装饰服务。
现在假设 thatService
使用已注入其中的 thatOtherService
。
我如何获得对 thatOtherService
的引用,以便我可以在我的 decoratorFN 想要添加到 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)在装饰器函数中注入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;
});
});
您可以在此 demo 中看到这两种方法的实际应用。