欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

AngularJS入門教程之服務(wù)(Service)

 更新時間:2016年07月27日 15:56:41   投稿:lqh  
本文主要介紹 AngularJS Service,這里整理了AngularJS Servic的詳細(xì)資料,并提供代碼實例,有需要的小伙伴可以參考下

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 地址。

實例

<!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>該實例使用了內(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)行結(jié)果:

當(dāng)前頁面的url:

http://www.runoob.com/try/try.php?filename=try_ng_services

該實例使用了內(nèi)建的 $location 服務(wù)獲取當(dāng)前頁面的 URL。

注意: $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ù)。

實例

使用 $http 服務(wù)向服務(wù)器請求數(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 服務(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>

運(yùn)行結(jié)果:

歡迎信息:

      歡迎訪問

$http 服務(wù)向服務(wù)器請求信息,返回的值放入變量 "myWelcome" 中。

以上是一個非常簡單的 $http 服務(wù)實例,更多 $http 服務(wù)應(yīng)用請查看 AngularJS Http 教程。

$timeout 服務(wù)

AngularJS $timeout 服務(wù)對應(yīng)了 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>

運(yùn)行結(jié)果:

兩秒后顯示信息:

How are you today?

$timeout 訪問在規(guī)定的毫秒數(shù)后執(zhí)行指定函數(shù)。

$interval 服務(wù)

AngularJS $interval 服務(wù)對應(yīng)了 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ù)或計算表達(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)行效果:

現(xiàn)在時間是:

下午3:41:09

$interval 訪問在指定的周期(以毫秒計)來調(diào)用函數(shù)或計

創(chuàng)建自定義服務(wù)

你可以創(chuàng)建自定義的訪問,鏈接到你的模塊中:

創(chuàng)建名為hexafy 的訪問:

app.service('hexafy', function() {
  this.myFunc = function (x) {
    return x.toString(16);
  }
});

要使用自定義的訪問,需要在定義過濾器的時候獨立添加:

實例

使用自定義的的服務(wù) hexafy 將一個數(shù)字轉(zhuǎn)換為16進(jìn)制數(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)行結(jié)果:

255 的16 進(jìn)制是:

f f

自定義服務(wù),用于轉(zhuǎn)換16進(jìn)制數(shù):

過濾器中,使用自定義服務(wù)

當(dāng)你創(chuàng)建了自定義服務(wù),并連接到你的應(yīng)用上后,你可以在控制器,指令,過濾器或其他服務(wù)中使用它。

在過濾器 myFormat 中使用服務(wù) 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">
在過濾器中使用服務(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)行效果:

在過濾器中使用服務(wù):

         f  f

在對象數(shù)組中獲取值時你可以使用過濾器:

創(chuàng)建服務(wù) 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>過濾器使用服務(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ù)組[255, 251, 200]值時使用過濾器:

  • ff
  • fb
  • c8

過濾器使用服務(wù)將10進(jìn)制轉(zhuǎn)換為16進(jìn)制。

以上就是對AngularJS 服務(wù)的資料整理,后續(xù)繼續(xù)補(bǔ)充,有需要的朋友參考下。

相關(guān)文章

最新評論