The Service,factory,provider,constant,value in angularjs1.x

Source: Internet
Author: User
Tags lua

Understanding the application scenarios and differences of Service,factory,provider,constant,value in angularjs1.x

Whether the service, factory or provider belong to the service

About service

Once a service is defined, it can be called from anywhere and can be stored in a dependent way until the application ends, such as

controller([‘service‘function(service){}]);

The controller will fail once the routing changes, and the next time you open the page you will need to reload the saved data.

Factory

Factory is the simplest way to create a service in Angularjs

Create Factory

By using factory we just need to return an object containing the data, the method, as follows

angular.module(‘MyApplication‘).factory(‘user‘, [‘$http‘function($http){    var‘http://localhost/api/v1/login‘;    return {        function(username, password) {            return$http.post(loginUrl, {                username: username,                password: password            });        },    };}]);
Call Factory

The above service can be invoked in the following ways

Angular.Module(' MyApplication '). Controller ([' $scope ',' user ', function($scope, user) {User.login (' name ',' Password '). Then( function(response) {if(Response.data.err_code = =0) {Console.log (' Login successful ')        }Else{Console.log (' Login failed ')        }    }, function(response) {Console.log (' Network request error ', response)});
Application Scenarios

In service, if we just need some method or data collection, we can simply create a service to meet the demand through factory.

Note: If you need to configure service in config, you cannot use factory to create

Service Creation Service

The service creates the service through the constructor, specifically implementing the same factory, such as the following

angular.module(‘MyApplication‘).service(‘user‘, [‘$http‘function($http){    var‘http://localhost/api/v1/login‘;    function(username, password) {        return$http.post(loginUrl, {            username: username,            password: password        });    };}]);
Invocation mode

It is called in the same way as factory

Angular.Module(' MyApplication '). Controller ([' $scope ',' user ', function($scope, user) {User.login (' name ',' Password '). Then( function(response) {if(Response.data.err_code = =0) {Console.log (' Login successful ')        }Else{Console.log (' Login failed ')        }    }, function(response) {Console.log (' Network request error ', response)});
Application Scenarios

As you can see, login here uses this. The way to create, in this scenario can write more business logic to control the data

Note: If you need to configure the service in config, the service is not available except factory

Provider

Provider is the lowest-level method of creating a service that can be called and configured in Config

Create Provider

Create provider, unlike factory, service, provider need to use this, $get to return methods and data

Angular.module (' MyApplication '). Provider (' user ', [' $http ', function($http){     This. loginurl =' Http://localhost/api/v1/login '; This. Setloginurl = function(URL){         This. loginurl = URL; } This.$Get= function($http) {        return{login: function(username, password) {                return$http. Post ( This. loginurl, {username:username, password:password});    },        }; }}]);
Invocation mode

Unlike factory and service, provider can be called in Config, as shown in

angular.module(‘MyApplication‘).config([‘user‘function(user){    user.setLoginUrl(‘http://localhost/api/v2/login‘);}]);

Normal invocation is the same as factory, which is called by service

Application Scenarios

Provider can be used when a particular data source needs to be configured by configuration, such as the possibility that the domain name of the data interaction address of the development and production environments may be inconsistent, and this can be done in this way

The Service,factory,provider,constant,value in angularjs1.x

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.