AngularJS ng-repeat遍歷輸出的用法
更新時間:2017年06月19日 16:23:32 作者:Darkersky
本篇文章主要介紹了AngularJS ng-repeat遍歷輸出的用法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
AngularJS ng-repeat遍歷輸出的用法,最近需要用,就順便發(fā)到隨筆上了
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>ng-repeat directive</title>
</head>
<body ng-app="myApp">
<table ng-controller="CartController">
<caption>我的購物車</caption>
<tr>
<th>序號</th>
<th>商品</th>
<th>單價</th>
<th>數(shù)量</th>
<th>金額</th>
<th>操作</th>
</tr>
<tr ng-repeat="item in items">
<td>{{$index + 1}}</td>
<td>{{item.name}}</td>
<td>{{item.price | currency}}</td>
<td><input ng-model="item.quantity"></td>
<td>{{item.quantity * item.price | currency}}</td>
<td>
<button ng-click="remove($index)">Remove</button>
</td>
</tr>
</table>
<script src="js/angular-1.3.0.14/angular.min.js"></script>
<script>
var app = angular.module('myApp', []);
app.controller('CartController',function($scope){
$scope.items = [
{name: "蘋果 iPhone7", quantity: 1, price: 5088.00},
{name: "榮耀Magic", quantity: 1, price: 3699.00},
{name: "vivo X9", quantity: 2, price: 2798.00}
];
//$index包含了ng-repeat過程中的循環(huán)計數(shù)
$scope.remove = function (index) {
$scope.items.splice(index, 1);
}
})
</script>
</body>
</html>
ng-repeat指令生命在需要循環(huán)內(nèi)容的元素上,items和控制器上的變量名對應(yīng),item是為數(shù)組中單個對象起的別名。
$index可以返回當前引用對象的序號,從0開始,另外還有$first、$middle、$last可以返回布爾值,用于告訴你
當前元素是否是集合中的第一個中間的最后一個元素。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- angularJS利用ng-repeat遍歷二維數(shù)組的實例代碼
- AngularJS ng-repeat指令中使用track by子語句解決重復數(shù)據(jù)遍歷錯誤問題
- Angular ng-repeat遍歷渲染完頁面后執(zhí)行其他操作詳細介紹
- Angular ng-repeat 對象和數(shù)組遍歷實例
- AngularJS遍歷獲取數(shù)組元素的方法示例
- angular ng-repeat數(shù)組中的數(shù)組實例
- AngularJS ng-repeat數(shù)組有重復值的解決方法
- AngularJS中比較兩個數(shù)組是否相同
- AngularJS使用ng-repeat遍歷二維數(shù)組元素的方法詳解
相關(guān)文章
angular.js+node.js實現(xiàn)下載圖片處理詳解
這篇文章主要介紹了angular.js+node.js實現(xiàn)下載圖片處理的相關(guān)資料,文中介紹的非常詳細,對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。2017-03-03
Angular 4.x中表單Reactive Forms詳解
這篇文章主要介紹了Angular 4.x中表單Reactive Forms的相關(guān)資料,文中通過示例代碼介紹的非常詳細,相信對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。2017-04-04

