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>我的購(gòu)物車</caption> <tr> <th>序號(hào)</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和控制器上的變量名對(duì)應(yīng),item是為數(shù)組中單個(gè)對(duì)象起的別名。
$index可以返回當(dāng)前引用對(duì)象的序號(hào),從0開始,另外還有$first、$middle、$last可以返回布爾值,用于告訴你
當(dāng)前元素是否是集合中的第一個(gè)中間的最后一個(gè)元素。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注