Angularjs Learning Note 3-Services and filters

Source: Internet
Author: User
Tags get definition

From the last blog has a period of time, because the recent company and personal things more, but also because of learning new knowledge so delayed, Some people say that Angularjs1.5 no one to use, no need to share, I personally feel that since the beginning I insist on writing him, some people still in use or maintenance project friends can have some help it;

Services: services provide a way to maintain data throughout the lifecycle of an application, communicate between controllers, and maintain data consistency.

Service Category:

constants and variable usage scenarios and differences:
1.value can not be injected in config, but constant can
2.value can be modified, but constant can not be modified, generally directly with the constant configuration of some data that need to be used frequently (the same name value cannot be modified).
3. You can use value () to register a service object or function and configure the data with constant ()
4. Value cannot be accessed within provider, constant can

Service (), Fanctory (), Provider (): Directly on the code more intuitively look at:

1  //1, Factory service statement:2     //inject factory, equivalent to the function call entry when the factory definition is injected3App.factory (' Testfactory ',function() {4         //1. Defining Objects5         varFactory = {};6         //2. Adding User attributes on Objects7Factory.username = "I am a factory service!" ";8         //2. Add the Commit function on the object property9Factory.submit =function() {TenConsole.log ("Factory Service Submission Method"); One         }; A         //3. Return Object -         returnFactory; -});
1  //2. Constructor Service Declaration2     //inject service, equivalent to the function instance when the service definition is injected3App.service (' Testservice ',function() {4         //Add a property or method binding through this5          This. Username = "I am the constructor service";6          This. Submit =function() {7Console.log ("Construct the service submission Method! ");8         }9});
 //3. Provider Services    //inject provider, equivalent to the invocation of a function instance injected into the $get definition within providerApp.provider (' Testprovider ',function() {         This. SetUrl = "";  This. $get =function() {            varthat = This; varPrivateprovider = {}; Privateprovider.user=function() {                return"I am the provider service method";            }; Privateprovider.submit=function() {Console.log ("I am the provider service Submission method"); Console.log ("I am the config configuration section:" +That.seturl); }            returnPrivateprovider; }    });

From the above code can be seen, service returns the definition of the function instance, so can not directly return the string, factory return is defined when the function call portal, so can return a string, personally feel that the two service is not too big difference, usually I compare with more is factory, Provider providers are lower than they are, using more flexible, mainly to provide external interface and the like can be used flexibly;

Decorator ():

App. Config (function($provide, Testproviderprovider) {Testproviderprovider.seturl= "This is config configuration content"; //Declaring decorator Services$provide. Decorator (' Testprovider ',function($delegate) {return{submit:function() {                    varStartDate =NewDate (); Console.log ("Commit time record start:" +startdate);                    $delegate. Submit (); varEndDate =NewDate (); Console.log ("Submit Time Record end:" +endDate); Console.log ("Total time to commit:" + (startdate-enddate) + "milliseconds"); }, User:function() {                    varresult =$delegate. User (); Console.log ("Result:" +result);    }            }        }); });

The service simply adds some custom code to the existing service through a horizontal slice to implement the custom content, such as adding a log code in the code above before and after the commit service executes, to record the run time, but for the caller without any effect. When using the service, it is noted that the above injected service name Testproviderprovider must end with provider;

Filter: filter is not explained in detail here, because this is relatively simple, there are many relevant examples on the Internet;

Angularjs Learning Note 3-Services and filters

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.