ANGULARJS---service (service/factory/provider)

Source: Internet
Author: User
Tags naming convention

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

    1. The implementation of the same business logic between brother controllers is achieved by inheriting from the parent controller. X
    2. Heap large amounts of unnecessary business logic and persistent data in $scope and controllers. X
    3. ......

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

    1. Service
    2. Factory
    3. 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)

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.