简而言之,装饰器可以描述如下:
装饰器功能拦截服务的创建,从而允许其覆盖或修改服务的行为。
它$provide
按角度使用服务,并修改或替换另一服务的实现
$provide.decorator('service to decorate',['$delegate', function($delegate) {
// $delegate - The original service instance,
// which can be replaced, monkey patched,
// configured, decorated or delegated to.
// ie here what is there in the 'service to decorate'
// This function will be invoked,
// when the service needs to be provided
// and should return the decorated service instance.
return $delegate;
}]);
例:
$provide.decorator('$log', ['$delegate', function($delegate) {
// This will change implementation of log.war to log.error
$delegate.warn = $delegate.error;
return $delegate;
}]);
应用领域
除了@JBland答案。