ANGULARJS Study notes--services

Source: Internet
Author: User


In Angularjs, a service is a function or object that can be used in a ANGULARJS application. Where the $location service can return the URL address of the current page. To use it, it needs to be defined in the controller and passed in as a parameter to the controller.

<! DOCTYPE html>

  

$http Service sends a request to the server to apply the data that the response server transmits.

var app = Angular.module ("myApp", []), App.controller ("Myctrl", Function ($scope, $http) {$http. Get ("welcome.html"). Then (function (response) {$scope. Mywelcome = Response.data;});});

  

$timeout

var app = Angular.module ("myApp", []), App.controller ("Myctrl", Function ($scope, $timeout) {$scope. MyHeader = "Hello World ", $timeout (function () {$scope. MyHeader =" How is you today? ";},2000);});

  

Create custom services: You can create custom services and link to your own modules, but when you access a custom service, you need to add and set dependencies independently when you define the controller. When a custom service is connected to its own app, it can be used in a controller/directive/filter or other service.

App.service (' Hexafy ', function () {This.myfunc = function (x) {return x.tostring (16);}}); App.controller (' Myctrl ', function ($scope, Hexafy) {$scope. Hex = Hexafy.myfunc (255);}); App.filter (' MyFormat ', [' Hexafy ', function (hexafy) {return function (x) {return hexafy.myfunc (x);};}]);

  

Not to be continued ...

ANGULARJS Study notes--services

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.