Efficient use of angular built-in services $http, $location and other _angularjs

Source: Internet
Author: User

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.

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.