装饰器机制是创建提供者的简写,该提供者获取先前的定义并返回它的新版本。
因此,您可以按如下方式模仿此功能:
module.provider('$log', function ($logProvider) {
// $logProvider here is the one previously defined, from 'ng'
// unless someone else is overriding too.
this.$get = function ($injector) {
// Invoke the original provider.
var $log = $injector.invoke($logProvider.$get);
var oldInfo = $log.info;
// Override the method.
$log.info = function info() {
oldInfo.apply(this, arguments);
// (not actually useful to alert arguments; just an example)
alert(arguments);
}
return $log;
}
});
当然,这会修改所有依赖项的对象,即使他们对标准的$log 接口非常满意,这意味着您无法直接在应用程序中查看哪些服务依赖于标准接口,哪些服务依赖于标准接口。取决于您的增强界面。如果您只想通过包装来更改现有方法的行为(例如,将日志发送到您的服务器,或者如您在问题中提到的那样显示烤面包机),这可能很有用,但可能是添加其他方法的冒险选择现有调用者不期望的方法,您可能会意外破坏与标准接口的兼容性。
相反,最好提供一项新服务,您可以在需要扩展的地方使用该服务,并将内置的$log 接口保留为标准。这样就更容易区分并避免标准调用者的意外行为变化。这看起来与上面类似,但在细节上略有不同:
module.provider('anotherLog', function ($logProvider) {
this.$get = function ($injector) {
var $log = $injector.invoke($logProvider.$get);
// Create a new object rather than extending the existing one in-place.
// This way the new method is only visible to callers that request
// 'anotherLog' instead of just '$log'.
var anotherLog = angular.extend(Object.create($log), {
sayHello: function sayHello() {
console.log('Hello!');
}
})
return anotherLog;
}
});
这两种方法都利用了这样一个事实,即在应用程序初始化期间,一个单独的“提供程序注入器”用于处理提供程序之间的依赖关系。这与创建应用程序后使用的主要$injector 不同。提供者注入器包含到目前为止已定义的所有提供者,但不会包含生成的服务,例如$log。提供者函数本身是通过提供者注入器注入的,而其$get 方法(如这些示例所示)是通过主$injector 注入的。
由于这种区别,如果您依赖任何其他服务来显示您的烤面包机,则必须依赖它们来实现 $get 方法本身,而不是整个提供程序。这将允许您访问主 $injector 中的服务,而不仅仅是来自提供程序注入器的提供程序。