AngularJS入門教程之服務(Service)
AngularJS 服務(Service)
AngularJS 中你可以創(chuàng)建自己的服務,或使用內(nèi)建服務。
什么是服務?
在 AngularJS 中,服務是一個函數(shù)或?qū)ο?,可在你?AngularJS 應用中使用。
AngularJS 內(nèi)建了30 多個服務。
有個 $location 服務,它可以返回當前頁面的 URL 地址。
實例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> </head> <body> <div ng-app="myApp" ng-controller="myCtrl"> <p> 當前頁面的url:</p> <h3>{{myUrl}}</h3> </div> <p>該實例使用了內(nèi)建的 $location 服務獲取當前頁面的 URL。</p> <script> var app = angular.module('myApp', []); app.controller('myCtrl', function($scope, $location) { $scope.myUrl = $location.absUrl(); }); </script> </body> </html>
運行結果:
當前頁面的url:
http://www.runoob.com/try/try.php?filename=try_ng_services
該實例使用了內(nèi)建的 $location 服務獲取當前頁面的 URL。
注意: $location 服務是作為一個參數(shù)傳遞到 controller 中。如果要使用它,需要在 controller 中定義。
為什么使用服務?
$http 是 AngularJS 應用中最常用的服務。服務向服務器發(fā)送請求,應用響應服務器傳送過來的數(shù)據(jù)。
AngularJS 會一直監(jiān)控應用,處理事件變化, AngularJS 使用 $location 服務比使用 window.location 對象更好。
$http 服務
$http 是 AngularJS 應用中最常用的服務。 服務向服務器發(fā)送請求,應用響應服務器傳送過來的數(shù)據(jù)。
實例
使用 $http 服務向服務器請求數(shù)據(jù):
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> </head> <body> <div ng-app="myApp" ng-controller="myCtrl"> <p>歡迎信息:</p> <>{{myWelcome}}<> </div> <p> $http 服務向服務器請求信息,返回的值放入變量 "myWelcome" 中。</p> <script> var app = angular.module('myApp', []); app.controller('myCtrl', function($scope, $http) { $http.get("welcome.htm").then(function (response) { $scope.myWelcome = response.data; }); }); </script>
運行結果:
歡迎信息:
歡迎訪問
$http 服務向服務器請求信息,返回的值放入變量 "myWelcome" 中。
以上是一個非常簡單的 $http 服務實例,更多 $http 服務應用請查看 AngularJS Http 教程。
$timeout 服務
AngularJS $timeout 服務對應了 JS window.setTimeout 函數(shù)。
實例
兩秒后顯示信息:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> </head> <body> <div ng-app="myApp" ng-controller="myCtrl"> <p>兩秒后顯示信息:</p> <h1>{{myHeader}}</h1> </div> <p>$timeout 訪問在規(guī)定的毫秒數(shù)后執(zhí)行指定函數(shù)。</p> <script> var app = angular.module('myApp', []); app.controller('myCtrl', function($scope, $timeout) { $scope.myHeader = "Hello World!"; $timeout(function () { $scope.myHeader = "How are you today?"; }, 2000); }); </script> </body> </html>
運行結果:
兩秒后顯示信息:
How are you today?
$timeout 訪問在規(guī)定的毫秒數(shù)后執(zhí)行指定函數(shù)。
$interval 服務
AngularJS $interval 服務對應了 JS window.setInterval 函數(shù)。
實例
每兩秒顯示信息:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> </head> <body> <div ng-app="myApp" ng-controller="myCtrl"> <p>現(xiàn)在時間是:</p> <h1>{{theTime}}</h1> </div> <p>$interval 訪問在指定的周期(以毫秒計)來調(diào)用函數(shù)或計算表達式。</p> <script> var app = angular.module('myApp', []); app.controller('myCtrl', function($scope, $interval) { $scope.theTime = new Date().toLocaleTimeString(); $interval(function () { $scope.theTime = new Date().toLocaleTimeString(); }, 1000); }); </script> </body> </html>
運行效果:
現(xiàn)在時間是:
下午3:41:09
$interval 訪問在指定的周期(以毫秒計)來調(diào)用函數(shù)或計
創(chuàng)建自定義服務
你可以創(chuàng)建自定義的訪問,鏈接到你的模塊中:
創(chuàng)建名為hexafy 的訪問:
app.service('hexafy', function() { this.myFunc = function (x) { return x.toString(16); } });
要使用自定義的訪問,需要在定義過濾器的時候獨立添加:
實例
使用自定義的的服務 hexafy 將一個數(shù)字轉(zhuǎn)換為16進制數(shù):
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> </head> <body> <div ng-app="myApp" ng-controller="myCtrl"> <p>255 的16進制是:</p> <h1>{{hex}}</h1> </div> <p>自定義服務,用于轉(zhuǎn)換16進制數(shù):</p> <script> var app = angular.module('myApp', []); app.service('hexafy', function() { this.myFunc = function (x) { return x.toString(16); } }); app.controller('myCtrl', function($scope, hexafy) { $scope.hex = hexafy.myFunc(255); }); </script> </body> </html>
運行結果:
255 的16 進制是:
f f
自定義服務,用于轉(zhuǎn)換16進制數(shù):
過濾器中,使用自定義服務
當你創(chuàng)建了自定義服務,并連接到你的應用上后,你可以在控制器,指令,過濾器或其他服務中使用它。
在過濾器 myFormat 中使用服務 hexafy:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> </head> <body> <div ng-app="myApp"> 在過濾器中使用服務: <h1>{{255 | myFormat}}</h1> </div> <script> var app = angular.module('myApp', []); app.service('hexafy', function() { this.myFunc = function (x) { return x.toString(16); } }); app.filter('myFormat',['hexafy', function(hexafy) { return function(x) { return hexafy.myFunc(x); }; }]); </script> </body> </html>
運行效果:
在過濾器中使用服務:
f f
在對象數(shù)組中獲取值時你可以使用過濾器:
創(chuàng)建服務 hexafy:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> </head> <body> <div ng-app="myApp" ng-controller="myCtrl"> <p>在獲取數(shù)組 [255, 251, 200] 值時使用過濾器:</p> <ul> <li ng-repeat="x in counts">{{x | myFormat}}</li> </ul> <p>過濾器使用服務將10進制轉(zhuǎn)換為16進制。</p> </div> <script> var app = angular.module('myApp', []); app.service('hexafy', function() { this.myFunc = function (x) { return x.toString(16); } }); app.filter('myFormat',['hexafy', function(hexafy) { return function(x) { return hexafy.myFunc(x); }; }]); app.controller('myCtrl', function($scope) { $scope.counts = [255, 251, 200]; }); </script> </body> </html>
運行效果:
在獲取數(shù)組[255, 251, 200]值時使用過濾器:
- ff
- fb
- c8
過濾器使用服務將10進制轉(zhuǎn)換為16進制。
以上就是對AngularJS 服務的資料整理,后續(xù)繼續(xù)補充,有需要的朋友參考下。
- angularJS Provider、factory、service詳解及實例代碼
- AngularJs Creating Services詳解及示例代碼
- 簡介AngularJS中使用factory和service的方法
- 簡介AngularJS中$http服務的用法
- AngularJS中$http服務常用的應用及參數(shù)
- Angularjs 自定義服務的三種方式(推薦)
- AngularJs自定義服務之實現(xiàn)簽名和加密
- AngularJS 服務詳細講解及示例代碼
- AngularJS入門教程之REST和定制服務詳解
- AngularJS通過$http和服務器通信詳解
- AngularJS服務service用法總結
相關文章
詳解AngularJS中$http緩存以及處理多個$http請求的方法
$http 是 AngularJS 中的一個核心服務,用于讀取遠程服務器的數(shù)據(jù),通過本文給大家介紹AngularJS中$http緩存以及處理多個$http請求的方法,希望的朋友一起學習吧2016-02-02Angular2 自定義validators的實現(xiàn)方法
angular 當需要form表單需要驗證時,angular自帶了許多校驗器,但是很多時候自帶的無法滿足業(yè)務需求,這時候就需要自定義的校驗器,下面通過本文給大家分享Angular2 自定義validators的實現(xiàn)方法,需要的朋友參考下吧2017-07-07Angular實現(xiàn)模版驅(qū)動表單的自定義校驗功能(密碼確認為例)
這篇文章主要介紹了Angular實現(xiàn)模版驅(qū)動表單的自定義校驗功能(密碼確認為例),本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2018-05-05angularJS利用ng-repeat遍歷二維數(shù)組的實例代碼
本篇文章主要介紹了angularJS利用ng-repeat遍歷二維數(shù)組的實例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06angular 用Observable實現(xiàn)異步調(diào)用的方法
這篇文章主要介紹了angular 用Observable實現(xiàn)異步調(diào)用的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12angularjs實現(xiàn)對表單輸入改變的監(jiān)控(ng-change和watch兩種方式)
這篇文章主要介紹了angularjs通過ng-change和watch兩種方式實現(xiàn)對表單輸入改變的監(jiān)控,需要的朋友可以參考下2018-08-08