At first Angularjs often write some code that is not elegant enough! I summed up a bit to see if you guys were shot!-----(this is only for service services and their related!)
The following practices are not very elegant
- The implementation of the same business logic between brother controllers is achieved by inheriting from the parent controller. X
- Heap large amounts of unnecessary business logic and persistent data in $scope and controllers. X
- ......
In fact, we should put the business logic and persisted data as far as possible in the service
From the memory performance point of view, only when the controller is needed to load, once you do not need to abandon, each refresh should be clear controller
A service is a singleton object that is created only when it is used and is valid for its lifetime (before the browser closes). And can be injected into different controllers.
The service is therefore more suitable for storing persisted data and most of the business logic.
Three ways to create services are available in angular
- Service
- Factory
- Provider
The service---Essence is to instantiate with ' new ' and then invoke the method and data for this in the service in the controller
var app = Angular.module (' myApp ', []), App.controller (' Rainctrl ', [' $scope ', ' FirstService ', function($scope, FirstService) { = firstservice.name;}]); App.service (function() { this. Name = ' RAIN_TDK ';})
But the average person doesn't just expose the data, it's supposed to be like this.
var app = Angular.module (' myApp ', []), App.controller (' Rainctrl ', [' $scope ', ' FirstService ', function($scope, FirstService) { = firstservice.getname ();}]); App.service (function() { var name = ' RAIN_TDK '; This function () { returnthis. Name = name; }})
Factory---Return must be a reference type
var app = Angular.module (' myApp ', []), App.controller (' Rainctrl ', [' $scope ', ' Secondservice ', function($scope, Secondservice) { = secondservice.name;}]); App.factory (function() { return { name:' RAIN_TDK ' }})
Provider
Provider is the only service that can pass the. config () function. If you want to do a module-wide configuration before the service object starts, you should choose provide ....
Note: There is a special naming convention when injecting provide into the Config function: Providename+provider, returned in $get to be called in the controller
Pros: You can modify the provide object in the. config () function before it can be passed to other controllers in the application
varApp = Angular.module (' myApp '), []); App.controller (' Rainctrl ', [' $scope ', ' Thirdservice ',function($scope, Thirdservice) {$scope. Name=Thirdservice.name;}]); App. Config (function(thirdserviceprovider) {thirdserviceprovider.name= ' RAIN_TDK2 ';}); App.provider (' Thirdservice ',function() { This. Name = ' Rain_tdk1 '; This. $get =function() { var_this = This; return{name: _this.name}})
Now look at the source
Can see what they three method is a virtue of haha haha ...
Small umbrella (RAIN_TDK) technology Limited ... If there is an error, please be correct. If in doubt, you are welcome to discuss ...
ANGULARJS---service (service/factory/provider)