Angular JS Learning Services (service)

Source: Internet
Author: User

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)

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.