Use Angular's built-in services, such as $ http and $ location, to efficiently use angular

Source: Internet
Author: User

Use Angular's built-in services, such as $ http and $ location, to efficiently use angular

AngularJS provides many built-in services for us. These built-in services can easily implement some common functions. The following is a summary of commonly used built-in services in Angular.
1. $ location service

$ Location is used to return the URL address of the current page. The sample code is as follows: var app = angular. module ('myapp', []); app. controller ('mermersctrl', function ($ scope, $ location) {$ scope. myUrl = $ location. absUrl ();});

Here, the myUrl variable is defined for the $ scope object, and then the $ location service is used to read the URL address and store it in myUrl.
2. $ http service
$ Http is the most common service in AngularJS. It is often used for server data transmission. In the following example, the Service sends a request to the server and the application responds to the data transmitted 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 () service and $ interval () Service
The functions of these two services correspond to the setTimeout () and setTimeInterval functions in javascript. An example of a simple real-time update time 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 by Angular, we can also define services by ourselves. Here is a basic code framework for defining services:

app.service('hexafy', function() {  this.myFunc = function (x) {   return x.toString(16);  } }); 

After defining a service, we can use it like using the built-in Angular service:

app.controller('myCtrl', function($scope, hexafy) {  $scope.hex = hexafy.myFunc(255); }); 

The above is a summary of commonly used built-in services in Angular, hoping to help you learn.

Articles you may be interested in:
  • Angularjs implements interaction and sharing with servers
  • Introduction to $ http service usage in AngularJS

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.