javascript - Why when I using track by $index will always remove the last element? -
i using ng-repeat track $index option, can not delete correct element array or object (ng-repeat remove last element).
this add button (add button created new table) :
$scope.current = 0; $scope.addhockpis = function () { $scope.repeattable.push('test' + $scope.current); $scope.current += 1; }; this table code:
<table id="addhockpistable{{$index}}" ng-repeat="table in repeattable track $index"> <td> <table> <tbody> <tr style="display: table-row"> <th style="height: 46px;"> <button type="button" role="button" ng-click="removeaddhockpis($index, table)"> <span class="ui-button-icon-primary ui-icon ui-icon-closethick"></span> </button> </th> </tr> </tbody> </table> </td> </table> and remove button js code :
$scope.removeaddhockpis = function (index, table) { $scope.repeattable.splice(index, 1); }; when delete second table array, deleting last table (so "test3")
it deleted "test1" value , new array:
deleted table test3 (like photo
)
angular not deleting last element, it's deleting element wanted , updating $index bindings. array contains 3 elements; when remove second, third element becomes second , index value becomes 1 instead of 2, since have 2 elements in array.
check this fiddle. notice when click on remove input, correct person removed , indexes updated.
Comments
Post a Comment