AngularJS ng-repeat遍歷輸出的用法
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>單價(jià)</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)計(jì)數(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可以返回當(dāng)前引用對象的序號,從0開始,另外還有$first、$middle、$last可以返回布爾值,用于告訴你
當(dāng)前元素是否是集合中的第一個中間的最后一個元素。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- angularJS利用ng-repeat遍歷二維數(shù)組的實(shí)例代碼
- AngularJS ng-repeat指令中使用track by子語句解決重復(fù)數(shù)據(jù)遍歷錯誤問題
- Angular ng-repeat遍歷渲染完頁面后執(zhí)行其他操作詳細(xì)介紹
- Angular ng-repeat 對象和數(shù)組遍歷實(shí)例
- AngularJS遍歷獲取數(shù)組元素的方法示例
- angular ng-repeat數(shù)組中的數(shù)組實(shí)例
- AngularJS ng-repeat數(shù)組有重復(fù)值的解決方法
- AngularJS中比較兩個數(shù)組是否相同
- AngularJS使用ng-repeat遍歷二維數(shù)組元素的方法詳解
相關(guān)文章
關(guān)于AngularJs數(shù)據(jù)的本地存儲詳解
本文主要介紹了每一個獨(dú)立的JS文件或者不同的控制器如何實(shí)現(xiàn)數(shù)據(jù)的共享與交互的方法。具有一定的參考價(jià)值,下面跟著小編一起來看下吧2017-01-01
使用AngularJS對表單提交內(nèi)容進(jìn)行驗(yàn)證的操作方法
AngularJS是一款優(yōu)秀的前端JS框架,已經(jīng)被用于Google的多款產(chǎn)品當(dāng)中。下面通過本文給大家分享使用AngularJS對表單提交內(nèi)容進(jìn)行驗(yàn)證的操作方法,需要的的朋友參考下吧2017-07-07
AngularJS基礎(chǔ) ng-readonly 指令簡單示例
本文主要介紹AngularJS ng-readonly 指令,這里對ng-readonly指令的資料做了整理,有學(xué)習(xí)AngularJS 指令的同學(xué)可以參考下2016-08-08
對比分析AngularJS中的$http.post與jQuery.post的區(qū)別
這篇文章主要給大家對比分析AngularJS中的$http.post與jQuery.post的區(qū)別,十分的詳細(xì),是篇非常不錯的文章,這里推薦給小伙伴們。2015-02-02
Bootstrap和Angularjs配合自制彈框的實(shí)例代碼
今天小編通過本文給大家分享Bootstrap和Angularjs配合自制彈框的實(shí)例代碼,代碼簡單易懂,有需要的朋友跟著小編一起學(xué)習(xí)2016-08-08
Angular.Js中ng-include指令的使用與實(shí)現(xiàn)
ng-include 指令用于包含外部的 HTML 文件。包含的內(nèi)容將作為指定元素的子節(jié)點(diǎn)。下面這篇文章主要給大家介紹了Angular.Js中ng-include指令的使用與實(shí)現(xiàn)的相關(guān)資料,文中介紹的非常詳細(xì),需要的朋友們下面來一起看看吧。2017-05-05

