The Provider,factory,service method in Angularjs

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.