Thursday, November 5, 2015

AngularJS - Provide Service

Ref:- https://code.angularjs.org/1.4.0/docs/api/auto/service/$provide

$provide

  1. - service in module auto
The $provide service has a number of methods for registering components with the $injector. Many of these functions are also exposed on angular.Module.
An Angular service is a singleton object created by a service factory. These service factories are functions which, in turn, are created by a service provider. The service providers are constructor functions. When instantiated they must contain a property called $get, which holds the service factory function.
When you request a service, the $injector is responsible for finding the correct service provider, instantiating it and then calling its $get service factory function to get the instance of the service.
Often services have no configuration options and there is no need to add methods to the service provider. The provider will be no more than a constructor function with a $get property. For these cases the $provide service has additional helper methods to register services without specifying a provider.
  • provider(provider) - registers a service provider with the $injector
  • constant(obj) - registers a value/object that can be accessed by providers and services.
  • value(obj) - registers a value/object that can only be accessed by services, not providers.
  • factory(fn) - registers a service factory function, fn, that will be wrapped in a service provider object, whose $get property will contain the given factory function.
  • service(class) - registers a constructor function, class that will be wrapped in a service provider object, whose $get property will instantiate a new object using the given constructor function.
See the individual methods for more information and examples.

Methods

provider(name, provider);

Register a provider function with the $injector. Provider functions are constructor functions, whose instances are responsible for "providing" a factory for a service.
Service provider names start with the name of the service they provide followed by Provider. For example, the $log service has a provider called $logProvider.
Service provider objects can have additional methods which allow configuration of the provider and its service. Importantly, you can configure what kind of service is created by the $get method, or how that service will act. For example, the $logProvider has a method debugEnabled which lets you specify whether the $log service will log debug messages to the console or not.

No comments:

Post a Comment