AngularJS service -- $ factory, service method in provide, angularjsfactory
The Service provides a way to keep data throughout the entire lifecycle of an application. It can communicate between controllers.
And ensures data consistency.
1 <! DOCTYPE html> 2
1. Register a service
Using the factory API of angular. module to create a service is the most common and flexible method.
12345678910 |
var myApp = angular.module('myApp', []); myApp.service('service1', function(){ return [1,2,3,4,5,6]; }); myApp.factory('factory1', function(){ return "111"; }); |
2. Use Services
The service can be used in the controller, command, filter, or another service by means of dependency declaration. AngularJS
It automatically handles instantiation and dependency loading issues at runtime as usual.
Var myApp = angular. module ('myapp', [], function ($ provide) {// 1, through $ provide. provider Custom Service $ provide. provider ('provider', function () {this. $ get = function () {return {message: 'xiaooping' }}}); // console. log (1); $ provide. provider ('provider2', function () {this. $ get = function () {return {message: 'xiaoguoping2' }}}); // 2. Use $ provide. service Custom service $ provide. service ('service1 ', function () {return [1, 2, 3, 4, 5, 6];}); // 3, by $ provide. factory custom 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. factory custom factory $ provide. factory ('factory1 ', function () {return "111" ;}); // a custom service can be called by other services and imported to the controller. myApp. controller ('firstcontroller', func Tion ($ 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 method used to register services, there are also some other APs that can help us reduce the amount of code under specific circumstances. There are five methods to create a service:
Factory () ---- the function can return data of any type, such as simple type, function, and object.
Movie service () ----- Function Array, object, and other data
The major difference between the constant () ---- value () method and the constant () method is that constants can be injected into the configuration function, but values cannot.
Value () ----- if the $ get method of the service returns a constant, there is no need to define a complete service that contains complex functions. You can use the value () function to conveniently register the service.
Incluprovider () ---- the provider is an object with the $ get () method. $ injector creates a service instance by calling the $ get method.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.