The difference between Service,factory,provider in Angularjs

Source: Internet
Author: User

1, Factory ()

The simplest way to create a service inside angular is to use the factory () method.

Factory () Let's define a service by returning an object that contains service methods and data. Within the service method we can inject services such as $http and $q.

In the service, factory () is a great choice when we only need a collection of methods and data and do not need to deal with complex logic.

Note: You cannot use the factory () method when configuring the service with. config ()

2. Service ()

Service () lets us create a service by constructors, and we can define the service using prototype mode instead of the original JavaScript object.

As with the factory () method, we can also see the injection of the service in the definition of the function. The service () method holds the object created by the constructor.

The service () method is well-suited for use in more functional control service

Note: You cannot use the service () method when you need to configure the service using. config ()

3, provider ()

Provider () is the lowest level of service creation, and the only way to configure service creation using the. config () method

Unlike the method mentioned above, we do a dependency injection in the definition of this $get () method.

When to use the provider () method

    1. We need to use the provider () when we want to configure the service before the app starts. For example, we need to configure services to use different backend processing in different deployment environments (development, demo, production).

    2. When we are going to publish open source provider () is also preferred to create a service, so that you can configure services using configuration instead of hard-coding the configuration data into the code.

The difference between Service,factory,provider 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.