ng-repeat :1.顯示一組HTML元素 2.顯示一個對象的全部屬性名及屬性值html
ng-repeat 指令能夠接受相似 「variable(變量) in arrayExpression (數組表達式)」 或(key , value) in objectExpression(對象表達式) 這些格式的參數。當參數爲數組時,數組中元素根據定義前後順序排列。數組
<!DOCTYPE html>
<html ng-app="notesApp">
<head>
<meta charset="UTF-8">
<title></title>
<script src="js/angular.min.js"></script>
</head>
<body ng-controller="MainCtrl as ctrl">
<div ng-repeat="(author,note) in ctrl.notes">
<span class="label">{{note.label}}</span> <!--取出屬性值-->
<span class="author" ng-bind="author"></span> <!--獲取對應屬性名-->
</div>
<script>
angular.module('notesApp',[]).controller('MainCtrl',[
function(){
var self=this;
self.notes={
shyam:{
id:1,
label:"first",
done:false,
},
Misko:{
id:2,
label:"second",
done:true,
},
brad:{
id:3,
label:"third",
done:false,
}
}
}])
</script>
</body>
</html>app
輸出:this
first shyamspa
second Miskohtm
third brad對象
ng-repeat中的輔助變量索引
元素的索引:ip
第一個,中間、最後一個、以及奇、偶性。it
每個以 $ 爲前綴的變量都是 angular JS 內置的 它能夠提供當前元素的某些統計信息
$first 、$middle 和 $last 都是布爾型變量,返回一個布爾值 ,判斷對應數組中的當前元素是不是第一個,中間 仍是最後。是返回true 不然返回false
$index 給出當前元素的索引值,出於數組中第幾個。
$odd 和 $even 表明着它的索引值的 奇、偶性。,能夠用來在 奇、偶行上顯示不一樣風格的元素,或其餘條件判斷。
<!DOCTYPE html>
<html ng-app="notesApp">
<head>
<meta charset="UTF-8">
<title></title>
<script src="js/angular.min.js"></script>
</head>
<body ng-controller="MainCtrl as ctrl">
<div ng-repeat="note in ctrl.notes">
<div> first Element:{{$first}}</div>
<div> middle Element:{{$middle}}</div>
<div> last Element:{{$last}}</div>
<div> Index of Element:{{$index}}</div>
<div> At Even Position:{{$even}}</div>
<div> At Odd Position:{{$odd}}</div>
<span class="label">{{note.label}}</span>
<span class="status" ng-bind="note.done"></span>
<br /><br />
</div>
<script>
angular.module('notesApp',[]).controller('MainCtrl',[
function(){
var self=this;
self.notes={
shyam:{
id:1,
label:"first",
done:false,
},
Misko:{
id:2,
label:"second", <!--$middle-->
done:true,
},
lala:{
id:3, <!--$middle-->
label:"third",
done:false,
},
brad:{
id:4,
label:"last",
done:true,
}
}
}])
</script>
</body>
</html>
輸出
first Element:true //是第一個元素
middle Element:false //不是中間元素
last Element:false //不是最後一個元素
Index of Element: 0 //元素索引爲0
At Even Position:true //處於奇數位置
At Odd Position:false //不處於偶數位置
first false
first Element:false
middle Element:true
last Element:false
Index of Element:1
At Even Position:false
At Odd Position:true
second true
first Element:false
middle Element:true
last Element:false
Index of Element:2
At Even Position:true
At Odd Position:false
third false
first Element:false
middle Element:false
last Element:true
Index of Element:3
At Even Position:false
At Odd Position:true
last true