Customizing the angular Service

Source: Internet
Author: User

Angular has built-in services such as $location services that interact with the browser's address bar, and $http services that interact with the server.

Naturally, you can customize the service yourself.

Using services can be a good place to interact and share state between multiple angular controllers, so creating a service that meets your current needs can often make it more efficient.

How do I create a angular service?

Angular provides a model object API to define the service, and three functions can be used to create a generic service

function definition

Provider (Name,object or constructor ()) A configurable service that creates more complex logic. If you pass an object as an argument,

So the object object must have a $get function, which needs to return the name of the service.

Otherwise, the angular character passes a constructor, and the call constructor returns the service instance object

Factory (name, $get Function ()) is a non-configurable service, and the creation logic is more complex. A function needs to be developed when this letter is called

The instance of the service is returned when the number is You can think of it as provider (name,{$get: $getfunction ()})

The form.

Services (Name,constructor ()) a non-configurable service, the creation of logic is relatively simple, with the above provider function constructor

parameter is similar, angular invokes it to create a service instance.

Provider

    Provider is bound to both factory and service, and you can enjoy the benefits of configuring provider before the injection system is ready to complete

Instance:

1 varMyapp=angular.module (' myApp '),[]);2Myapp.provider (' Greeter ',function(){3     varsalutation= ' Hello ';4      This. setsalutation=function(s) {5salutation=s;6     }7 8     functionGreeter (a) {9          This. greet=function(){Ten             returnsalutation+ ' +A; One         } A     } -  -      This. $get =function(a) { the         return NewGreeter (a); -     }; -});

Factory

   If there is a class or object that needs to be given some logic or arguments before it can be initialized, then the factory interface is used.

Instance

1Myapp.factory (' Greeter ',function(salut) {2     return NewGreeter (salut);3 });4 5 functionGreeter (salutation) {6      This. greet=function(name) {7         returnsalutation+ ' +name;8     };9}

Service

The difference between factory and service is that the service invokes the function passed to it, and then returns the execution result; factory uses the

The "new" keyword to invoke the construction method passed to it and return the result

Instance

1 myapp.factory (' greeter ', greeter); 2 3 function Greeter (salutation) {4     this. greet=function(name) {5         return salutation+ ' +  Name; 6     }; 7 }

Customizing the angular Service

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.