AngularJS 服務(wù)詳細(xì)講解及示例代碼
AngularJS支持使用服務(wù)的體系結(jié)構(gòu)“關(guān)注點(diǎn)分離”的概念。服務(wù)是JavaScript函數(shù),并負(fù)責(zé)只做一個(gè)特定的任務(wù)。這也使得他們即維護(hù)和測試的單獨(dú)實(shí)體??刂破鳎^濾器可以調(diào)用它們作為需求的基礎(chǔ)。服務(wù)使用AngularJS的依賴注入機(jī)制注入正常。
AngularJS提供例如許多內(nèi)在的服務(wù),如:$http, $route, $window, $location等。每個(gè)服務(wù)負(fù)責(zé)例如一個(gè)特定的任務(wù),$http是用來創(chuàng)建AJAX調(diào)用,以獲得服務(wù)器的數(shù)據(jù)。 $route用來定義路由信息等。內(nèi)置的服務(wù)總是前綴$符號。
有兩種方法來創(chuàng)建服務(wù)。
工廠
服務(wù)
使用工廠方法
使用工廠方法,我們先定義一個(gè)工廠,然后分配方法給它。
var mainApp = angular.module("mainApp", []);
mainApp.factory('MathService', function() {
var factory = {};
factory.multiply = function(a, b) {
return a * b
}
return factory;
});
使用服務(wù)方法
使用服務(wù)的方法,我們定義了一個(gè)服務(wù),然后分配方法。還注入已經(jīng)可用的服務(wù)。
mainApp.service('CalcService', function(MathService){
this.square = function(a) {
return MathService.multiply(a,a);
}
});
例子
下面的例子將展示上述所有指令。
testAngularJS.html
<html>
<head>
<title>Angular JS Forms</title>
</head>
<body>
<h2>AngularJS Sample Application</h2>
<div ng-app="mainApp" ng-controller="CalcController">
<p>Enter a number: <input type="number" ng-model="number" />
<button ng-click="square()">X<sup>2</sup></button>
<p>Result: {{result}}</p>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>
<script>
var mainApp = angular.module("mainApp", []);
mainApp.factory('MathService', function() {
var factory = {};
factory.multiply = function(a, b) {
return a * b
}
return factory;
});
mainApp.service('CalcService', function(MathService){
this.square = function(a) {
return MathService.multiply(a,a);
}
});
mainApp.controller('CalcController', function($scope, CalcService) {
$scope.square = function() {
$scope.result = CalcService.square($scope.number);
}
});
</script>
</body>
</html>
結(jié)果
在Web瀏覽器打開textAngularJS.html??吹浇Y(jié)果如下。

以上就是對AngularJS 服務(wù)的基礎(chǔ)資料整理,后續(xù)繼續(xù)整理相關(guān)資料,謝謝大家對本站的支持!
- AngularJS入門教程之服務(wù)(Service)
- angularJS Provider、factory、service詳解及實(shí)例代碼
- AngularJs Creating Services詳解及示例代碼
- 簡介AngularJS中使用factory和service的方法
- 簡介AngularJS中$http服務(wù)的用法
- AngularJS中$http服務(wù)常用的應(yīng)用及參數(shù)
- Angularjs 自定義服務(wù)的三種方式(推薦)
- AngularJs自定義服務(wù)之實(shí)現(xiàn)簽名和加密
- AngularJS入門教程之REST和定制服務(wù)詳解
- AngularJS通過$http和服務(wù)器通信詳解
- AngularJS服務(wù)service用法總結(jié)
相關(guān)文章
AngularJS實(shí)現(xiàn)的select二級聯(lián)動下拉菜單功能示例
這篇文章主要介紹了AngularJS實(shí)現(xiàn)的select二級聯(lián)動下拉菜單功能,結(jié)合完整實(shí)例形式分析了AngularJS實(shí)現(xiàn)二級聯(lián)動菜單的具體操作步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-10-10
Angularjs實(shí)現(xiàn)數(shù)組隨機(jī)排序的方法
今天小編就為大家分享一篇Angularjs實(shí)現(xiàn)數(shù)組隨機(jī)排序的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10
Angular.js中$resource高大上的數(shù)據(jù)交互詳解
這篇文章主要給大家介紹了關(guān)于Angular.js中$resource高大上的數(shù)據(jù)交互的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用angular.js具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來一起看看吧。2017-07-07
ionic4+angular7+cordova上傳圖片功能的實(shí)例代碼
ionic是一個(gè)垮平臺開發(fā)框架,可通過web技術(shù)開發(fā)出多平臺的應(yīng)用。這篇文章主要介紹了ionic4+angular7+cordova上傳圖片功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-06-06
詳解AngularJs HTTP響應(yīng)攔截器實(shí)現(xiàn)登陸、權(quán)限校驗(yàn)
本篇文章主要介紹了AngularJs HTTP響應(yīng)攔截器實(shí)現(xiàn)登陸、權(quán)限校驗(yàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-04-04

