这种类型的装饰器与服务和工厂一起工作.我希望它也能与提供商合作.我尝试过以下方法来装饰ui-router的$stateProvider:
app.config(function($provide) { $provide.decorator('$state',function ($delegate) { return $delegate; }); });
它应该工作一样吗?请参阅plunk @
@L_502_1@
原文链接:https://www.f2er.com/angularjs/141221.htmlvar 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(); });