AngularJS 服務(wù)(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 地址。
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $location) {
$scope.myUrl = $location.absUrl();
});
注意 $location 服務(wù)是作為一個參數(shù)傳遞到 controller 中。如果要使用它,需要在 controller 中定義。
$http 服務(wù)
使用 $http 服務(wù)向服務(wù)器請求數(shù)據(jù):
$http 是 AngularJS 應(yīng)用中最常用的服務(wù)。 服務(wù)向服務(wù)器發(fā)送請求,應(yīng)用響應(yīng)服務(wù)器傳送過來的數(shù)據(jù)。
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
$http.get("welcome.htm").then(function (response) {
$scope.myWelcome = response.data;
});
});
$timeout 服務(wù)
AngularJS $timeout 服務(wù)對應(yīng)了 JS window.setTimeout 函數(shù)。
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $timeout) {
$scope.myHeader = "Hello World!";
$timeout(function () {
$scope.myHeader = "How are you today?";
}, 2000);
});
$interval 服務(wù)
AngularJS $interval 服務(wù)對應(yīng)了 JS window.setInterval 函數(shù)。
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $interval) {
$scope.theTime = new Date().toLocaleTimeString();
$interval(function () {
$scope.theTime = new Date().toLocaleTimeString();
}, 1000);
});
創(chuàng)建自定義服務(wù)
你可以創(chuàng)建訪問自定義服務(wù),鏈接到你的模塊中:
創(chuàng)建名為hexafy 的訪問:
app.service('hexafy', function() {
this.myFunc = function (x) {
return x.toString(16);
}
});
要使用訪問自定義服務(wù),需要在定義過濾器的時候獨立添加:
使用自定義的的服務(wù) hexafy 將一個數(shù)字轉(zhuǎn)換為16進制數(shù):
app.controller('myCtrl', function($scope, hexafy) {
$scope.hex = hexafy.myFunc(255);
});
完整代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://cdn.static.runoob.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>自定義服務(wù),用于轉(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>
過濾器中,使用自定義服務(wù)
當(dāng)你創(chuàng)建了自定義服務(wù),并連接到你的應(yīng)用上后,你可以在控制器,指令,過濾器或其他服務(wù)中使用它。
在過濾器 myFormat 中使用服務(wù) hexafy:
app.filter('myFormat',['hexafy', function(hexafy) {
return function(x) {
return hexafy.myFunc(x);
};
}]);
在對象數(shù)組中獲取值時你可以使用過濾器:
創(chuàng)建服務(wù) hexafy:
<ul>
<li ng-repeat="x in counts">{{x | myFormat}}</li>
</ul>
完整代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://cdn.static.runoob.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進制轉(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>
參考資料
菜鳥教程
今天學(xué)的較少,家里事情較多。明天出發(fā)上學(xué)了。。。后面再補了!嘿嘿,觀眾老爺就我一個,加油!