In angularjs how to decorate the $stateProvider Pr

2019-04-07 02:41发布

问题:

This type of decorator works with services and factories. I expected it to work with providers as well. I've tried the following to decorate ui-router's $stateProvider:

app.config(function($provide) {
  $provide.decorator('$state', function ($delegate) {
    return $delegate;
  });
});

Here's a demo plunk

回答1:

It should work just the same? See 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();
});