AngularJS的ng-repeat迴圈中如何獲得被迴圈列表的索引值
阿新 • • 發佈:2018-11-21
使用angularJS框架的話都需要引入angular.min.js檔案,這個想必各位大佬都懂得,我就不多說了。
這裡主要說說AngularJS的ng-repeat迴圈中如何獲得被迴圈列表的索引值。
假如有一個表格:
程式碼如下:
<table class="table table-bordered table-striped table-hover dataTable"> <thead> <tr> <th class="sorting">規格選項</th> <th class="sorting">排序</th> <th class="sorting">操作</th> </thead> <tbody> <tr ng-repeat="pojo in entity.specificationOptionList"> <td> <input class="form-control" placeholder="規格選項" ng-model="pojo.optionName"> </td> <td> <input class="form-control" placeholder="排序" ng-model="pojo.orders"> </td> <td> <button type="button" class="btn btn-default" title="刪除" ng-click="deleteTableRow($index)"><i class="fa fa-trash-o"></i> 刪除</button> </td> </tr> </tbody> </table>
我點選刪除按鈕,就相當於把specificationOptionList集合去除一列資料,呼叫controller.js檔案中呼叫$scope.entity.specificationOptionList.splice(index,1)。
那index的值從何得到呢,我們只需要在ng-repeat迴圈中,刪除按鈕中的ng-click="deleteTableRow()方法裡傳入$index就可以了。