Angular service study notes

 

1. service is a singleton object - to ensure that each access is the same object

2. Implement controller and directive data sharing.

 

When you first try Angular, it's natural to stuff controllers and scopes with unnecessary logic. It must be realized early that the controller layer should be very thin; that is, most of the business logic and persistent data in the application should be placed in the service .

 

Angular provides 3 ways to create and register our own service.

1、 Factory

2、 Service

3、 Provider

 

 

/**
* Define the ApiService service
* Function: Dedicated to sending post and get requests to the server
* */
angular.module("myapp")
	.factory('myService1', ["$window", "$http", "$q", "$log",function($window, $http, $q, $log) {
		console.log("I am the content of the closure part");
		// public api
		return {
			getUserData:function(){
				console.log("I am getUserData");
			},
			getOrgList:function(){
				console.log("I am getOrgList");
			},
			delUserById:function(userId){
				console.log("I will delete the data: " + userId);
			},
			// as shared data (data can be cached)
			shareData : {
				name:"huangbiao"
			}
		};
	}])
	//The current method is service
	.service("myService2",["$window", "$http", "$q", "$log",function($window, $http, $q, $log) {
//                console.dir($q);
	   this.serviceMethod = function(){
		   console.log("serviceMethod")
	   };
		this.serviceMethod2 = function(){
			console.log("serviceMethod2")
		};
		this.shareData = {
			name : "liumei",
			age : 18
		}
	}])
//            .provider("myServicePro",["$window", "$http", "$q", "$log",function($window, $http, $q, $log) {
	//Providers are the only kind of service you can pass into the .config() function. The provider should be used when you want to perform module-wide configuration before the service object is enabled.
	// So there is no way to pass in other parameters for injection in this way
	.provider("myServicePro",[ function() {
//                console.dir($q);
		console.log("I am the closure part");

		this.$get = function(){
			console.log("I am closure part 2");
			var resultService = {};
			resultService.myServiceProMethod = function(){
				console.log("serviceMethod")
			};
			resultService.myServiceProMethod2 = function(){
				console.log("serviceMethod2")
			};
			resultService.myServiceProShareData = {
				name : "huangbiao",
				age : 28
			}
			return resultService;
		};
	}]);
	
	
angular.module("myapp")
	.controller("AccordionDemoCtrl", ["$scope","myService1","myService2","myServicePro" ,
	function($scope,myService1,myService2,myServicePro) {
			myService1.getUserData();
			myService1.getOrgList();
			myService1.delUserById(123);
			$scope.before = function(){
				alert(myService1.shareData.name);
			};
			$scope.change = function(){
				myService1.shareData.name = "I have changed";
			};

			console.dir(myService2);
			console.dir(myServicePro);

		}])

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326822101&siteId=291194637