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