$provide (service in module AUTO )

Description

Use $provide to register new providers with the $injector. The providers are the factories for the instance. The providers share the same name as the instance they create with the Provider suffixed to them.

A provider is an object with a $get() method. The injector calls the $get method to create a new instance of a service. The Provider can have additional methods which would allow for configuration of the provider.

  function GreetProvider() {
    var salutation = 'Hello';

    this.salutation = function(text) {
      salutation = text;
    };

    this.$get = function() {
      return function (name) {
        return salutation + ' ' + name + '!';
      };
    };
  }

  describe('Greeter', function(){

    beforeEach(module(function($provide) {
      $provide.provider('greet', GreetProvider);
    });

    it('should greet', inject(function(greet) {
      expect(greet('angular')).toEqual('Hello angular!');
    }));

    it('should allow configuration of salutation', function() {
      module(function(greetProvider) {
        greetProvider.salutation('Ahoj');
      });
      inject(function(greet) {
        expect(greet('angular')).toEqual('Ahoj angular!');
      });
    )};

  });

Methods