Home>

Introduction to angularjs:

angularjs extends html with new attributes and expressions.

angularjs can build a single page application (spas:single page applications).

Angularjs is very easy to learn.

You can customize services in three ways ($provider, $factory, $service) in angularjs.Here are the different implementations:

//define module, inject $provide in module
var starterapp=angular.module ("starter.controllers", [], function ($provide) {
//The first way:use provider's custom service
$provide.provider ("getuserinfoservice", function () {
this. $get=function () {
var userinfo=[{
"username":"Zhang San 0","usernick":"Little Flower 0","age":25
}, {
"username":"Zhang San 1","usernick":"Little Flower 1","age":26
}];
return userinfo;
}
});
$provide.factory ("", function () {});
$provide.service ("", function () {});
});
//The second way (inject $provide in the module's config method)
starterapp.config (["$provide", function ($provide) {
//Use provided provider custom service (return object,String, service, and must be returned via the $get method)
$provide.provider ("getuseraddressservice", function () {
var _useraddress="";
var service=();
this. $get=function () {
service.setaddress=function (useraddress) {
_useraddress=useraddress;
}
service.getaddress=function () {
return _useraddress;
}
return service;
}
});
//Use the provided factory custom service (return object,Service, string)
$provide.factory ("servicename1", ["$http", function ($http) {
//var service=();
//service.getname=function () {
//return "Zhang San";
//}
//return service;
//
return "Ah big";
}]);
//Use provided service to customize the service (return object,service)
$provide.service ("servicename2", ["$http", function ($http) {
//return {
//"name":"aa"
//};
//Method can be defined directly through this
this.getname=function () {
return "Zhang San";
}
}])
}]);
//The third method (the provider, service, and factory methods of the module recommend the third method)
starterapp.provider ("servicename3", function () {
this. $get=function () {
return "Define a service directly through the module's provider method";
}
});
starterapp.factory ("servicename4", function () {
return "Define a service directly through the module's factory method";
});
starterapp.service ("servicename5", function () {
return {
"message":"Define a service directly via the module's service method"
}
});
  • Previous JavaScript waterfall layout implementation code
  • Next AngularJs custom service implementation signing and encryption