ANGULARJS provides us with a number of built-in services that allow you to easily implement common functions. The following is a summary of the built-in services commonly used in angular.
1. $location Services
$location service is used to return the URL address of the current page, the sample code is as follows:
var app = Angular.module (' myApp ', []);
App.controller (' Customersctrl ', function ($scope, $location) {
$scope. Myurl = $location. Absurl ();
});
This defines the Myurl variable for the $scope object and then uses the $location service to read the URL address and store it in the Myurl.
2. $http Services
$http is the most commonly used service in Angularjs, and it is often used for data transfer from the server. In the example below, the service sends a request to the server that responds to data sent by the server.
var app = Angular.module (' myApp ', []);
App.controller (' Myctrl ', function ($scope, $http) {
$http. Get ("welcome.htm"). Then (function (response) {
$ Scope.mywelcome = Response.data;
});
3. $timeout () services and $interval () services
The functions of these two services correspond to the settimeout () and Settimeinterval functions in JavaScript. A simple real-time update time example is as follows:
App.controller (' Myctrl ', function ($scope, $interval) {
$scope. thetime = new Date (). tolocaletimestring ();
$interval (function () {
$scope. thetime = new Date (). tolocaletimestring ();
}, 1000);
});
In addition to the built-in services provided in angular, we can define our own services, using service, and here is a basic code framework for defining services:
App.service (' Hexafy ', function () {
This.myfunc = function (x) {return
x.tostring ();
}
});
Once the service is defined, we can use it like the built-in angular service:
App.controller (' Myctrl ', function ($scope, hexafy) {
$scope. Hex = Hexafy.myfunc (255);
The above is for the angular of the commonly used built-in services for the summary, I hope to help you learn.