$provide
(service in module 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 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!');
});
)};
});
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.
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.A short hand for configuring services if only $get method is required.
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}).
{Object}
– registered provider instance
Register a provider for a service. The providers can be retrieved and can have additional configuration methods.
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.{Object}
– registered provider instance
A short hand for registering service of given class.
name – {string} –
The name of the instance.
constructor – {Function} –
A class (constructor function) that will be instantiated.
{Object}
– registered provider instance
A short hand for configuring services if the $get method is a constant.
name – {string} –
The name of the instance.
value – {*} –
The value.
{Object}
– registered provider instance