$provide
AUTO
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!'); }); });
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.
Param | Type | Details |
---|---|---|
name | string | The name of the service to decorate. |
decorator | function() | This function will be invoked when the service needs to be
instantiated. The function is called using the
|
A short hand for configuring services if only $get
method is required.
Param | Type | Details |
---|---|---|
name | string | The name of the instance. |
$getFn | function() | The $getFn for the instance creation. Internally this is a short hand for
|
Object | registered provider instance |
Register a provider for a service. The providers can be retrieved and can have additional configuration methods.
Param | Type | Details |
---|---|---|
name | string | The name of the instance. NOTE: the provider will be available under |
provider | Objectfunction() | If the provider is:
|
Object | registered provider instance |