Although angular provides many useful service, in some special applications, we find it useful to write custom service. If we want to do this, we first need to register a service factory method in module, which can be passed through the Module.factory API (http://docs.angularjs.org/api/ Angular.module) or directly through the $provide API (Http://docs.angularjs.org/api/AUTO. $provide) in the module configuration method.
All angular service participates in Di (http://www.jb51.net/article/91775.htm) and can register themselves by using the name (ID) in the Angular di system (injector) You can also declare dependencies on existing service in other factory methods.
First, registering Services
To register a service, we have to have a module and make this server part of the module. We can then register the service through the module API or in the module configuration function. The following pseudocode will show the two ways of registering.
Using the Angular.module API:
var mymodule = angular.module (' MyModule ', []);
Mymodule.factory (' Serviceid ', function () {
var someservice;
Factory method body, build Someservice return
someservice;
};
Use $provide service:
Angular.module (' MyModule ', [],function ($provide) {
$provide. Factory (' Serviceid ', function () {
var Someservice;
Factory method body, build Someservice return someservice;});
Note that we do not need to register a service instance, on the contrary, the factory method is instantiated when it is invoked.
Second, dependencies
Service can not only be relied upon, but also can have its own dependence. You can specify dependencies in the parameters of the factory method. Read (http://www.jb51.net/article/91775.htm) more about di in angular, the use of array tags, and $inject attributes, making di declarations simpler. (Read more about the "Di" in angular and the "use of" notation and $inject property to make DI annotation minification-p Roof ...)
Here is a very simple example of service. This service relies on the $window service (passed by the factory method parameter) and has only one method. This service simply stores all notifications, and after the third, the service will display all notifications via Window.alert.
Third, instantiating angular Services
All service in angular is deferred instantiation (lazily). This means that the service is created only if it is dependent on other instantiated service or application components that depend on it. In other words, angular will not instantiate the service until it is directly or indirectly requested.
Iv. Services as Singletons
Finally, we must realize that all angular service is a single case application. This means that there are only one instance of a given service in each injector. Since angular is extremely annoying to destroy global state, it is possible to create multiple injector so that each has an instance of a specified service, which is rarely required in addition to strong requirements in testing.
The above is about angular Services data collation, follow-up continue to supplement the relevant information, thank you for your support of this site!