AngularJS Services--$provide factory, service methods

Source: Internet
Author: User

Service provides a way to maintain data over the lifetime of an application, and it can pass through the controller
To ensure consistency of data.



1 <!DOCTYPE HTML>2 <HTML>3 <Head>4 <MetaCharSet= "Utf-8">5 <title>2, ANGULARJS Custom Service</title>6 <Scriptsrc=".. /js/angularjs.js "></Script>7 <Scriptsrc=".. /js/index3.js "></Script>8 9 </Head>Ten <Body> One <!--ANGULARJS Data binding, two-way binding, binding between M,v,c - A - - <DivNg-app= "MYAPP"Ng-controller= "Firstcontroller"> the <inputtype= "text"Ng-model= "Name"value="" /> - {{name}} - </Div> - + - </Body> + </HTML>

1. Register a Service

Creating services using Angular.module's Factory API is the most common and flexible way

var myApp = angular.module (' MyApp ', []); Myapp.service (' Service1 ', function () {return [1,2,3,4,5,6];}); Myapp.factory (' Factory1 ', function () {return "111";});

2. Use of the service

You can use a service in a controller, Directive, filter, or another service by relying on a declarative approach. AngularJS
will automatically handle instantiation and dependency loading at run time, as usual.

varMyApp = Angular.module (' MyApp ', [],function($provide) {//1. Custom Service via $provide.provider$provide. Provider (' Provider ',function(){         This. $get =function(){            return{message:' Xiaoguoping '            }        }    }); //Console.log (1);$provide. Provider (' Provider2 ',function(){         This. $get =function(){            return{message:' Xiaoguoping2 '            }        }    });//2. Custom Service via $provide.service$provide. Service (' Service1 ',function(){            return[1,2,3,4,5,6]; });//3. Custom Factory through $provide.factory$provide. Factory (' Factory1 ',function(){            return"111";    }); //myapp.service (' Service1 ', function () {    //return [1,2,3,4,5,6];    // });    //myapp.factory (' Factory1 ', function () { //return "111"; //     });//3. Custom Factory through $provide.factory$provide. Factory (' Factory1 ',function(){            return"111"; });});//Custom services can be called by other services into the controller.Myapp.controller (' Firstcontroller ',function($scope, provider, Provider2, Service1, Factory1) {$scope. Name= ' Zhang San ';    Console.log (provider);    Console.log (PROVIDER2);    Console.log (Service1); Console.log (Factory1);});In ANGULARJS applications, the factory () method is the most common way to register a service, while some other APS can help us reduce the amount of code in certain situations.There are 5 ways to create a service:

? Factory ()----function can return arbitrary types of data, such as simple types, functions, and even objects
? Service ()-----Array of functions, objects, and other data
? The main difference between the constant ()----the value () method and the constant () method is that a constant can be injected into a configuration function and the value is not.
? Value ()-----If the $get method of the service returns a constant, then there is no need to define a full service with complex functionality, which can be conveniently registered with the value () function.
? The provider ()----provider is an object with a $get () method, $injector creates a service instance by calling the $get method.

AngularJS Services--$provide factory, service methods

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.