AngularJS Service
AngularJS 中你可以創(chuàng)建自己的服務(wù),或使用內(nèi)建服務(wù)。
什么是服務(wù)?
在 AngularJS 中,服務(wù)是一個函數(shù)或?qū)ο?,可在你?AngularJS 應(yīng)用中使用。
AngularJS 內(nèi)建了30 多個服務(wù)。
有個 $location 服務(wù),它可以返回當(dāng)前頁面的 URL 地址。
實(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> 當(dāng)前頁面的url:</p> <h3>{{myUrl}}</h3> </div> <p>該實(shí)例使用了內(nèi)建的 $location 服務(wù)獲取當(dāng)前頁面的 URL。</p> <script> var app = angular.module('myApp', []); app.controller('myCtrl', function($scope, $location) { $scope.myUrl = $location.absUrl(); }); </script> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
注意 $location 服務(wù)是作為一個參數(shù)傳遞到 controller 中。如果要使用它,需要在 controller 中定義。
為什么使用服務(wù)?
$http 是 AngularJS 應(yīng)用中最常用的服務(wù)。服務(wù)向服務(wù)器發(fā)送請求,應(yīng)用響應(yīng)服務(wù)器傳送過來的數(shù)據(jù)。
AngularJS 會一直監(jiān)控應(yīng)用,處理事件變化, AngularJS 使用 $location 服務(wù)比使用 window.location 對象更好。
$http 服務(wù)
$http 是 AngularJS 應(yīng)用中最常用的服務(wù)。 服務(wù)向服務(wù)器發(fā)送請求,應(yīng)用響應(yīng)服務(wù)器傳送過來的數(shù)據(jù)。
實(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>{{myWelcome}}</h1> </div> <p> $http 服務(wù)向服務(wù)器請求信息,返回的值放入變量 "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> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
以上是一個非常簡單的 $http 服務(wù)實(shí)例,更多 $http 服務(wù)應(yīng)用請查看 AngularJS Http 教程。
$timeout 服務(wù)
AngularJS $timeout 服務(wù)對應(yīng)了 JS window.setTimeout 函數(shù)。
實(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>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
$interval 服務(wù)
AngularJS $interval 服務(wù)對應(yīng)了 JS window.setInterval 函數(shù)。
實(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ù)或計算表達(dá)式。</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>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
創(chuàng)建自定義服務(wù)
你可以創(chuàng)建訪問自定義服務(wù),鏈接到你的模塊中:
創(chuàng)建名為hexafy 的訪問:
this.myFunc = function (x) {
return x.toString(16);
}
});
要使用訪問自定義服務(wù),需要在定義過濾器的時候獨(dú)立添加:
實(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進(jìn)制是:</p> <h1>{{hex}}</h1> </div> <p>自定義服務(wù),用于轉(zhuǎn)換16進(jìn)制數(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>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
過濾器中,使用自定義服務(wù)
當(dāng)你創(chuàng)建了自定義服務(wù),并連接到你的應(yīng)用上后,你可以在控制器,指令,過濾器或其他服務(wù)中使用它。
實(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"> 在過濾器中使用服務(wù): <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>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
在對象數(shù)組中獲取值時你可以使用過濾器:
實(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>在獲取數(shù)組 [255, 251, 200] 值時使用過濾器:</p> <ul> <li ng-repeat="x in counts">{{x | myFormat}}</li> </ul> <p>過濾器使用服務(wù)將10進(jìn)制轉(zhuǎn)換為16進(jìn)制。</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>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例