此类装饰器适用于服务和工厂。我希望它也能与提供商合作。我尝试过以下方法来装饰 ui-router 的$stateProvider
:
app.config(function($provide) {
$provide.decorator('$state', function ($delegate) {
return $delegate;
});
});
答案 0 :(得分:2)
应该一样吗?请参阅plunk @ http://plnkr.co/edit/rSFo1xCoRHjWmrSjJBN1
var app = angular.module('plunker', []);
app.provider('provider', function () {
this.$get = function () {
var provider = {};
var value = 'test';
provider.get = function() {
return value;
}
provider.set = function(param) {
value = param;
}
return provider;
}
});
app.config(function($provide) {
$provide.decorator('provider', function ($delegate) {
$delegate.set('delegate');
return $delegate;
});
});
app.controller('MainCtrl', function($scope, provider) {
$scope.name = provider.get();
});