AngularJS語(yǔ)法詳解
模板和數(shù)據(jù)的基本運(yùn)作流程如下:
用戶請(qǐng)求應(yīng)用起始頁(yè)面
用戶的瀏覽器向服務(wù)器發(fā)起一次http連接,然后加載index.html頁(yè)面,這個(gè)頁(yè)面包含了模板
angular被加載到頁(yè)面中,等待頁(yè)面加載完成,查找ng-app指令,用來(lái)定義模板的邊界
angular遍歷模板,查找指定和綁定關(guān)系,將觸發(fā)一些列動(dòng)作:注冊(cè)監(jiān)聽(tīng)器、執(zhí)行一些DOM操作、從服務(wù)器獲取初始化數(shù)據(jù)。最后,應(yīng)用將會(huì)啟動(dòng)起來(lái),并將模板轉(zhuǎn)換成DOM視圖
連接到服務(wù)器去加載需要展示給用戶的其他數(shù)據(jù)
顯示文本
一種使用{{}}形式,如{{greeting}} 第二種ng-bind="greeting"
使用第一種,未被渲染的頁(yè)面可能會(huì)被用戶看到,index頁(yè)面建議使用第二種,其余的頁(yè)面可以使用第一種
表單輸入
<html ng-app>
<head>
<title>表單</title>
<script type="text/javascript" src="angular.min.js"></script>
<script type="text/javascript">
function StartUpController($scope) {
$scope.funding = {startingEstimate:0};
computeNeeded = function() {
$scope.funding.needed = $scope.funding.startingEstimate * 10;
};
$scope.$watch('funding.startingEstimate',computeNeeded); //watch model的變化
}
</script>
</head>
<body>
<form ng-controller="StartUpController">
Starting: <input ng-change="computeNeeded()" ng-model="funding.startingEstimate"> //change的時(shí)候調(diào)用函數(shù)
Recommendation: {{funding.needed}}
</form>
</body>
</html>
在某些情況下,我們不想一有變化就立刻做出動(dòng)作,而是要進(jìn)行等待。例如:
<html ng-app>
<head>
<title>表單</title>
<script type="text/javascript" src="angular.min.js"></script>
<script type="text/javascript">
function StartUpController($scope) {
$scope.funding = {startingEstimate:0};
computeNeeded = function() {
$scope.funding.needed = $scope.funding.startingEstimate * 10;
};
$scope.$watch('funding.startingEstimate',computeNeeded);//watch監(jiān)視一個(gè)表達(dá)式,當(dāng)這個(gè)表達(dá)式發(fā)生變化時(shí)就會(huì)調(diào)用一個(gè)回調(diào)函數(shù)
$scope.requestFunding = function() {
window.alert("Sorry,please get more customers first.")
};
}
</script>
</head>
<body>
<form ng-submit="requestFunding()" ng-controller="StartUpController"> //ng-submit
Starting: <input ng-change="computeNeeded()" ng-model="funding.startingEstimate">
Recommendation: {{funding.needed}}
<button>Fund my startup!</button>
</form>
</body>
</html>
非表單提交型的交互,以click為例
<html ng-app>
<head>
<title>表單</title>
<script type="text/javascript" src="angular.min.js"></script>
<script type="text/javascript">
function StartUpController($scope) {
$scope.funding = {startingEstimate:0};
computeNeeded = function() {
$scope.funding.needed = $scope.funding.startingEstimate * 10;
};
$scope.$watch('funding.startingEstimate',computeNeeded);
$scope.requestFunding = function() {
window.alert("Sorry,please get more customers first.")
};
$scope.reset = function() {
$scope.funding.startingEstimate = 0;
};
}
</script>
</head>
<body>
<form ng-controller="StartUpController">
Starting: <input ng-change="computeNeeded()" ng-model="funding.startingEstimate">
Recommendation: {{funding.needed}}
<button ng-click="requestFunding()">Fund my startup!</button>
<button ng-click="reset()">Reset</button>
</form>
</body>
</html>
列表、表格以及其他迭代型元素
ng-repeat會(huì)通過(guò)$index返回當(dāng)前引用的元素序號(hào)。 示例代碼如下:
<html ng-app>
<head>
<title>表單</title>
<script type="text/javascript" src="angular.min.js"></script>
<script type="text/javascript">
var students = [{name:'Mary',score:10},{name:'Jerry',score:20},{name:'Jack',score:30}]
function StudentListController($scope) {
$scope.students = students;
}
</script>
</head>
<body>
<table ng-controller="StudentListController">
<tr ng-repeat='student in students'>
<td>{{$index+1}}</td>
<td>{{student.name}}</td>
<td>{{student.score}}</td>
</tr>
</table>
</body>
</html>
隱藏與顯示
ng-show和ng-hide功能是等價(jià)的,但是運(yùn)行效果正好相反。
<html ng-app>
<head>
<script type="text/javascript" src="angular.min.js"></script>
<script>
function DeathrayMenuController($scope) {
$scope.menuState = {show:false };//這里換成menuState.show = false 效果就顯示不出來(lái)了。以后聲明變量還是放在{}里面吧
$scope.toggleMenu = function() {
$scope.menuState.show = !$scope.menuState.show;
};
}
</script>
</head>
<body>
<div ng-controller='DeathrayMenuController'>
<button ng-click='toggleMenu()'>Toggle Menu</button>
<ul ng-show='menuState.show'>
<li ng-click='stun()'>Stun</li>
<li ng-click='disintegrate()'>Disintegrate</li>
<li ng-click='erase()'>Erase from history</li>
</ul>
</div>
</body>
</html>
css類(lèi)和樣式
ng-class和ng-style都可以接受一個(gè)表達(dá)式,表達(dá)式執(zhí)行的結(jié)果可能是如下取值之一:
表示css類(lèi)名的字符串,以空格分隔
css類(lèi)名數(shù)組
css類(lèi)名到布爾值的映射
代碼示例如下:
<html ng-app>
<head>
<style type="text/css">
.error {
background-color: red;
}
.warning {
background-color: yellow;
}
</style>
<script type="text/javascript" src="angular.min.js"></script>
<script>
function HeaderController($scope) {
$scope.isError = false;
$scope.isWarning = false;
$scope.showError = function() {
$scope.messageText = "Error!!!!"
$scope.isError = true;
$scope.isWarning = false;
}
$scope.showWarning = function() {
$scope.messageText = "Warning!!!"
$scope.isWarning = true;
$scope.isError = true;
}
}
</script>
</head>
<body>
<div ng-controller="HeaderController">
<div ng-class="{error:isError,warning:isWarning}">{{messageText}}</div>
<button ng-click="showError()">Error</button>
<button ng-click="showWarning()">Warning</button>
</div>
</body>
</html>
css類(lèi)名到布爾值的映射
示例代碼如下:
<html ng-app>
<head>
<style type="text/css">
.selected {
background-color: lightgreen;
}
</style>
<script type="text/javascript" src="angular.min.js"></script>
<script>
function Restaurant($scope) {
$scope.list = [{name:"The Handsome",cuisine:"BBQ"},{name:"Green",cuisine:"Salads"},{name:"House",cuisine:'Seafood'}];
$scope.selectRestaurant = function(row) {
$scope.selectedRow = row;
}
}
</script>
</head>
<body>
<table ng-controller="Restaurant">
<tr ng-repeat='restaurant in list' ng-click='selectRestaurant($index)' ng-class='{selected: $index==selectedRow}'> //css類(lèi)名到布爾值的映射,當(dāng)模型屬性selectedRow的值等于ng-repeat中得$index時(shí),selectd樣式就會(huì)被設(shè)置到那一行
<td>{{restaurant.name}}</td>
<td>{{restaurant.cuisine}}</td>
</tr>
</table>
</body>
</html>
相關(guān)文章
Angular實(shí)現(xiàn)雙向折疊列表組件的示例代碼
本篇文章主要介紹了Angular實(shí)現(xiàn)雙向折疊列表組件的示例代碼,分為左右兩組,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11詳解Angular中的自定義服務(wù)Service、Provider以及Factory
本篇文章主要介紹了詳解Angular中的自定義服務(wù)Service、Provider以及Factory,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04angularJs提交文本框數(shù)據(jù)到后臺(tái)的方法
今天小編就為大家分享一篇angularJs提交文本框數(shù)據(jù)到后臺(tái)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10angular動(dòng)態(tài)刪除ng-repaeat添加的dom節(jié)點(diǎn)的方法
本篇文章主要介紹了angular動(dòng)態(tài)刪除ng-repaeat添加的dom節(jié)點(diǎn)的方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-07-07AngularJS ng-repeat指令及Ajax的應(yīng)用實(shí)例分析
這篇文章主要介紹了AngularJS ng-repeat指令及Ajax的應(yīng)用,結(jié)合實(shí)例形式分析了ng-repeat指令的功能及ajax請(qǐng)求交互相關(guān)操作技巧,需要的朋友可以參考下2017-07-07Angular2中監(jiān)聽(tīng)數(shù)據(jù)更新的方法
今天小編就為大家分享一篇Angular2中監(jiān)聽(tīng)數(shù)據(jù)更新的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08Angular4學(xué)習(xí)教程之DOM屬性綁定詳解
這篇文章主要給大家介紹了關(guān)于Angular4學(xué)習(xí)教程之DOM屬性綁定的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01