AngularJS例子 ng-repeat遍历输出 通过js的splice方法删除当前行

 1 <!doctype html>
 2 <html>
 3 <head>
 4     <meta charset="utf-8">
 5     <title>ng-repeat directive</title>
 6 </head>
 7 <body ng-app="myApp">
 8 <table ng-controller="CartController">
 9     <caption>我的购物车</caption>
10     <tr>
11         <th>序号</th>
12         <th>商品</th>
13         <th>单价</th>
14         <th>数量</th>
15         <th>金额</th>
16         <th>操作</th>
17     </tr>
18     <tr ng-repeat="item in items">
19         <td>{{$index + 1}}</td>
20         <td>{{item.name}}</td>
21         <td>{{item.price | currency}}</td>
22         <td><input ng-model="item.quantity"></td>
23         <td>{{item.quantity * item.price | currency}}</td>
24         <td>
25             <button ng-click="remove($index)">Remove</button>
26         </td>
27     </tr>
28 </table>
29 
30 <script src="js/angular-1.3.0.14/angular.min.js"></script>
31 <script>
32     var app = angular.module('myApp', []);
33     app.controller('CartController',function($scope){
34         $scope.items = [
35             {name: "苹果 iPhone7", quantity: 1, price: 5088.00},
36             {name: "荣耀Magic", quantity: 1, price: 3699.00},
37             {name: "vivo X9", quantity: 2, price: 2798.00}
38         ];
39         //$index包含了ng-repeat过程中的循环计数
40         $scope.remove = function (index) {
41             $scope.items.splice(index, 1);
42         }
43     })
44 </script>
45 </body>
46 </html>

 

ng-repeat指令生命在需要循环内容的元素上,items和控制器上的变量名对应,item是为数组中单个对象起的别名。

$index可以返回当前引用对象的序号,从0开始,另外还有$first、$middle、$last可以返回布尔值,用于告诉你

当前元素是否是集合中的第一个中间的最后一个元素。

转载于:https://www.cnblogs.com/daryl123/p/6231468.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值