1.AngularJS, you can create your own services, or use built-in services;
2. In Angularjs, a service is a function or object that can be used in your ANGULARJS application;
Angularjs has built more than 30 services; there is a $location service that can return the URL of the current page;
var app=angular.module (' myApp ', []);
App.controller (' Customersctrl ', function ($scope, $location) {
$scope. Myurl= $location. Absurl ();
});
3. $http is the most commonly used service in ANGULARJS application, the service sends the request to the server and applies the data transmitted by the response server;
ANGULARJS will always monitor the application, handle event changes, Angularjs use the $location service rather than using Windows. Location object is better;
var app=angular (' myApp ', []);
App.controller (' Myctrl ', function ($scope, $http) {
$http. Get ("welcome.htm"). Then (function (response) {
$scope. Mywelcome=response.data;
});
});
4. $timeout Service: In response to the JS Window.settimeout function:
var app=angular.module (' myApp ', []);
App.controller (' Myctrl ', function ($scope, $timeout) {
$scope. myheader= "Hello world!";
$timeout (function () {
$scope. myheader= "How is you today?";
},2000);
});
5. $interval service: Corresponding to JS window.setinterval function
var app=angular.module (' myApp ', []);
App.controller (' Myctrl ', function ($scope, $interval) {
$scope. Thetime=new Date (). tolocaletimestring ();
$interval (function () {
$scope. Thetime=new Date (). tolocaletimestring ();
},1000);
});
6. Create a custom service: You can create access to custom services, link to your module;
App.service (' Hexafy ', function () {
This.myfunc=function (x) {
return x.tostring (16);
}
});
With access to custom services, you need to add them independently when you define filters:
App.controller (' Myctrl ', function ($scope, Hexafy) {
$scope. Hex=hexafy.myfunc (255);
});
7. Filter, use custom services: When you create a custom service and connect to your app, you can use it in controllers, directives, filters or other services;
Use the service Hexafy in the filter MyFormat:
App.filter (' MyFormat ', [' Hexafy ', function (Hexafy) {
return function (x) {
return Hexafy.myfunc (x);
};
}]);
Angular JS Learning Services (service)