Making a service provider in Angular

I am working on a library that extends some functions in ui.routerto create sets of application states. I am currently managing this by defining a new provider in my library (call him enhancedState) in the form:

myApp.provider('enhancedState', EnhancedState);

EnhancedState.$inject = ['$stateProvider'];
function EnhancedState ($stateProvider) {
  this.$stateProvider = $stateProvider;
}
EnhancedState.prototype = {
  /* fun new methods that wrap $stateProvider */
  $get: ['$state', function ($state) {
    /* maybe wrap some $state functionality too */
    return $state;
  }
};

Then I can use mine enhancedStateProviderin my application configuration to make fun new state definitions, following the advanced features of my library.

I would prefer, however, to decorate the class directly $stateProvider. I know the Angular utility $provider.decorate(), but, as far as I can tell, it can only be used to decorate created instances of the service, not the provider.

Has anyone successfully used it to decorate suppliers or is aware of a similar mechanism for this in Angular 1.x?

!

+4
1

AngularJS ( decorator ). plunker. , .

:

app.config(function($provide) {
  $provide.decorator('$state', function($delegate) {
    $delegate.customMethod = function(a) {
      console.log(a);
    };
    return $delegate;
  });
});
+5

Source: https://habr.com/ru/post/1617520/


All Articles