Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In angularjs how to decorate the $stateProvider Provider?

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

like image 510
Gil Birman Avatar asked Jun 17 '14 20:06

Gil Birman


1 Answers

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();
});
like image 176
Mikko Viitala Avatar answered Oct 21 '22 05:10

Mikko Viitala