国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

AngularJS Service

AngularJS 中你可以創(chuàng)建自己的服務(wù),或使用內(nèi)建服務(wù)。


什么是服務(wù)?

在 AngularJS 中,服務(wù)是一個(gè)函數(shù)或?qū)ο?,可在你?AngularJS 應(yīng)用中使用。

AngularJS 內(nèi)建了30 多個(gè)服務(wù)。

有個(gè) $location 服務(wù),它可以返回當(dāng)前頁(yè)面的 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)前頁(yè)面的url:</p>
<h3>{{myUrl}}</h3>
</div>

<p>該實(shí)例使用了內(nèi)建的 $location 服務(wù)獲取當(dāng)前頁(yè)面的 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ù)是作為一個(gè)參數(shù)傳遞到 controller 中。如果要使用它,需要在 controller 中定義。


為什么使用服務(wù)?

$http 是 AngularJS 應(yīng)用中最常用的服務(wù)。服務(wù)向服務(wù)器發(fā)送請(qǐng)求,應(yīng)用響應(yīng)服務(wù)器傳送過(guò)來(lái)的數(shù)據(jù)。

AngularJS 會(huì)一直監(jiān)控應(yīng)用,處理事件變化, AngularJS 使用 $location 服務(wù)比使用 window.location 對(duì)象更好。


$http 服務(wù)

$http 是 AngularJS 應(yīng)用中最常用的服務(wù)。 服務(wù)向服務(wù)器發(fā)送請(qǐng)求,應(yīng)用響應(yīng)服務(wù)器傳送過(guò)來(lái)的數(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ù)器請(qǐng)求信息,返回的值放入變量 "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í)例

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


$timeout 服務(wù)

AngularJS $timeout 服務(wù)對(duì)應(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 訪問(wèn)在規(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ù)對(duì)應(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)在時(shí)間是:</p>

<h1>{{theTime}}</h1>

</div>

<p>$interval 訪問(wèn)在指定的周期(以毫秒計(jì))來(lái)調(diào)用函數(shù)或計(jì)算表達(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èn)自定義服務(wù),鏈接到你的模塊中:

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

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

要使用訪問(wèn)自定義服務(wù),需要在定義過(guò)濾器的時(shí)候獨(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í)例


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

當(dāng)你創(chuàng)建了自定義服務(wù),并連接到你的應(yīng)用上后,你可以在控制器,指令,過(guò)濾器或其他服務(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">
在過(guò)濾器中使用服務(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í)例

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

實(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] 值時(shí)使用過(guò)濾器:</p>

<ul>
  <li ng-repeat="x in counts">{{x | myFormat}}</li>
</ul>

<p>過(guò)濾器使用服務(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í)例