# `$provide` (service in module `AUTO` )

Improve this doc

## 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 `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

- ### constant(name, value)

  A constant value, but unlike `value` it can be injected into configuration function (other modules) and it is not interceptable by `decorator`.

  #### Parameters

  - `name – {string} –`

    The name of the constant.

  - `value – {*} –`

    The constant value.

  #### Returns

  `{Object}` –

  registered instance

- ### decorator(name, decorator)

  Decoration of service, allows the decorator to intercept the service instance creation. The returned instance may be the original instance, or a new instance which delegates to the original instance.

  #### Parameters

  - `name – {string} –`

    The name of the service to decorate.

  - `decorator – {function()} –`

    This function will be invoked when the service needs to be instanciated. The function is called using the `injector.invoke` method and is therefore fully injectable. Local injection arguments:

    - `$delegate` - The original service instance, which can be monkey patched, configured, decorated or delegated to.

- ### factory(name, $getFn)

  A short hand for configuring services if only `$get` method is required.

  #### Parameters

  - `name – {string} –`

    The name of the instance.

  - `$getFn – {function()} –`

    The $getFn for the instance creation. Internally this is a short hand for `$provide.provider(name, {$get: $getFn})`.

  #### Returns

  `{Object}` –

  registered provider instance

- ### provider(name, provider)

  Register a provider for a service. The providers can be retrieved and can have additional configuration methods.

  #### Parameters

  - `name – {string} –`

    The name of the instance. NOTE: the provider will be available under `name + 'Provider'` key.

  - `provider – {(Object|function())} –`

    If the provider is:

    - `Object`: then it should have a `$get` method. The `$get` method will be invoked using `$injector.invoke()` when an instance needs to be created.
    - `Constructor`: a new instance of the provider will be created using `$injector.instantiate()`, then treated as `object`.

  #### Returns

  `{Object}` –

  registered provider instance

- ### service(name, constructor)

  A short hand for registering service of given class.

  #### Parameters

  - `name – {string} –`

    The name of the instance.

  - `constructor – {Function} –`

    A class (constructor function) that will be instantiated.

  #### Returns

  `{Object}` –

  registered provider instance

- ### value(name, value)

  A short hand for configuring services if the `$get` method is a constant.

  #### Parameters

  - `name – {string} –`

    The name of the instance.

  - `value – {*} –`

    The value.

  #### Returns

  `{Object}` –

  registered provider instance