Angularjs Basic concepts and usage services

Source: Internet
Author: User

First, the service

Services provide a way to maintain data over the life cycle of an application, enabling communication between controllers and maintaining data consistency.

1. The service is a singleton object that will only be instantiated once (by $injector) in each application;

2. The service provides an interface that centralizes the methods associated with a particular function, which is used to encapsulate a common method, request background data, and process data back to the controller;

3. Once the service is registered, it can be referenced and loaded into the runtime as a dependency.

Ii. 5 ways to create a service

1.factory Service

The factory () method is the quickest way to create and configure a service.

App.factory ('name', function () {return obj})

name is the name of the service, the second parameter passes in a function, and the function needs to have a return value, obj, to return an object. The actual service being injected is this object.

Serviceapp.factory (' myconfig ',function() {    var myname = ' Code_bunny ';     var age =N;     var id = 1;     return {        name:myname,        age:age,        function() {            return ID        }}    );

2.service Service

You can register a service that supports constructors by using service ()

App.service (' name ',constructor)

Name is the name of the service, andconstructor is a constructor.

Serviceapp.service (' myconfig ',function() {    var myname = ' Code_bunny ';     var age =N;     var id = 1;     this. Name = myname;     this. Age = Age ;      This function () {        return  ID    }});

3.provider Service

So the service factory is created by the $provide service, and all methods of creating the service are built on top of the provider method.

name is the name of the service, the second parameter takes a function, the function returns an object, the returned object, such as the $get method, $get method must return an object, obj, which is the service that is actually injected.

The return value of the second parameter of the provider service must have a $get method (in addition to $get, there can be other methods, the following example will say), $get method is equivalent to the second parameter of the factory service, and finally return an object, this object is really injected service:

App.provider (' name ',function() {.... return {... $get:function() {... return obj}}     )

 serviceapp.provider (' Myconfig ', function   ( { return   {$get:  function   () { var  myname           = ' Code_bunny ' ;            var  age = 12;            var  id = 1;                return   {name:myname,                   Age:age, GetId:  function   () {        return   ID}} }   }});

These two types of writing are the same as before, which can be seen as a shorthand.

Only if we want to configure the service in the config () function, we must use provider () to define the service.

Serviceapp.provider (' Myconfig ',function(){    varID = 1; return{setID:function(NewID) {ID=NewID}, $get:function(){            varmyname = ' Code_bunny '; varAge = 12; return{name:myname, age:age, GetId:function(){                    return(ID}}}}); Serviceapp.config (function(Myconfigprovider) {Myconfigprovider.setid (2)});

4.constant Service

Constant () generally save the constants

App.constant (' name ', obj)

Name is the name of the service, andobj is a JSON object .

Serviceapp.constant (' Myconfig ', {    name:' Code_bunny ', age    :    getId:  function() {        return 1    }});

5.value Service

App.value (' name ', obj)

Name is the name of the service, andobj is a JSON object.

Serviceapp.value (' Myconfig ', {    name:' Code_bunny ', age    :    getId:  function() {        return 1    }});

6. Decoration Service Decorator

$provide Service provides the ability to intercept a service instance when it is created, to extend or override the service, and so on.

Serviceapp.value (' Myconfig ', {    name:' Code_bunny ', age    :    getId:  function() {        return 1    }});
Serviceapp.config (function($provide) {    $provide. Decorator (' myconfig ',function ($delegate) {        = ' 100w ';         return $delegate    })});

Angularjs Basic concepts and usage services

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.