AngularJS基礎(chǔ)學(xué)習(xí)-02

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é)了。。。后面再補了!嘿嘿,觀眾老爺就我一個,加油!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容