Defining a service using the provider method in $provide
As already known, the module definition is angular.module (NAME,[REQUIRES],CONFIGFN); The CONFIGFN parameter is the configuration service. The process of NG for service involves its dependency injection mechanism. Angularjs is an automatic dependency injection mechanism with $provider objects. $provide. Provider is a way to define a service. The injection mechanism calls the resulting object as a parameter by invoking the $get method of the provider.
<! doctype html>Defines a service named HelloService, which, through the $get method, allows HelloService to be equal to the object returned by $get. In the controller Controller1, using the HelloService service, it is written in the parameter list. Here you can see that $scope is also a service. Print out the HelloService in the console with the following results:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/6E/FB/wKioL1WOW9GhqHePAABn47kDtaY009.jpg "title=" Console view.png "alt=" Wkiol1wow9ghqhepaabn47kdtay009.jpg "/>
This method generally does not work, because there are more convenient methods, such as the factory and service methods in $provide.
Factory and service methods in the $provide
factory methods and service methods can be seen as shorthand for provider methods, which omit the write $get method, as follows:
Factory method
<! doctype html>For The factory method, the return can be any type, so it is no problem to return the string here.
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/6E/FF/wKiom1WOX2KB2WEFAABkaqd0IyE432.jpg "title=" Console view1.png "alt=" Wkiom1wox2kb2wefaabkaqd0iye432.jpg "/>
Service method
<! doctype html>The service method can only return a reference type (array, object, and so on), and if it is other types, it will not get the content. This is the difference between factory and service.
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/6E/FF/wKiom1WOYHvB3CoFAAB0rD6bZ3Q336.jpg "title=" Console view2.png "alt=" Wkiom1woyhvb3cofaab0rd6bz3q336.jpg "/>
In addition, the factory and service methods also have a shortcut in the module, using:
var module1 = angular.module (' module name ', ...); Module1.factory (); Module1.service ();
The Provider,factory,service method in Angularjs