Three ways to Angularjs custom services (recommended) _angularjs

Source: Internet
Author: User

ANGULARJS Introduction:

Angularjs extends HTML through new properties and expressions.

Angularjs can build a single page application (Spas:single page applications).

Angularjs is very easy to learn.

The ANGULARJS can be customized in three ways ($provider, $factory, $service), and the following are different implementation forms:

Define module, injected into module $provide var starterapp = angular.module (' starter.controllers ', [],function ($provide) {//The first way: Custom Service $provide with provide provider. Provider (' Getuserinfoservice ', function () {this. $get = function () {var userInfo = [{]
UserName ': ' 30 ', ' Usernick ': ' Floret 0 ', ' age ': '},{' userName ': ' Zhang 31 ', ' Usernick ': ' Floret 1 ', ' Age ': 26}];
return userInfo;
}
});
$provide. Factory (' ", function () {});
$provide. Service (' ", function () {});
}); The second approach (injected $provide in the Config method of module) starterapp.config ([' $provide ', function ($provide) {///provide provider custom service ( Returns the object, string, service, and must be returned through the $get method $provide. Provider (' Getuseraddressservice ', function () {var _useraddress = '; var service = {
}; this. $get = function () {service.setaddress = function (useraddress) {_useraddress = useraddress;} service.getaddress = Fu
Nction () {return _useraddress;} return service;
}
});
Use provide's factory custom service (return object, Service, String) $provide. Factory (' serviceName1 ', [' $http ', function ($http) {//var service = {}; Service.getname = FunctiOn () {//return ' John ';//}//return service;
Return "Ah whispering big";
}]); Use provide service custom services (returns objects, services) $provide. Service (' serviceName2 ', [' $http ', function ($http) {//return {//' name ': '
AA '//};
You can define method This.getname = function () {return ' John ';}} directly via this.
}]); The Third Way (module's provider, service, factory method recommends a third) Starterapp.provider (' ServiceName3 ', function () {this. $get =
function () {return ' defines the service ' directly through the provider method of module;
Starterapp.factory (' ServiceName4 ', function () {return ' defines service directly through module's factory method ';}); Starterapp.service (' ServiceName5 ', function () {return {' message ': ' Define services directly through module's service method '}});

The above is a small set to introduce the ANGULARJS custom service three ways summary, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

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.