我是 Angular 的新手,我想获取特定行的索引以在其上执行函数 hidestuff()
,我将 item.id
传递给该函数,然后我想要隐藏包含此 ID 的行女巫......但我如何通过行索引来删除整行?
html:
<tr ng-repeat="item in ItemsByPage[currentPage]">
<td>
<div ng-model="tid"
ng-hide="hidden"
ng-class="{fade: startFade}">
{{item.id}}
</div>
</td>
<td>
<div editable-text="item.name"
onaftersave='inlineupdateName(item.id,item.name)'
ng-model="tname" data-n='item.name'>
{{item.name}}
</div>
</td>
<td>
<div editable-text="item.phone"
onaftersave='inlineupdatephone(item.id,item.phone)'
ng-model="tphone">
{{item.phone}}
</div>
</td>
</tr>
<input type="text" ng-model="delId" class="form-control"
placeholder="Enter user id to delete th user">
<button ng-click="deleteuser(delId)" type="button"
class="btn btn-primary">
Delete User
</button>
js:
$scope.hideStuff = function (delId) {
$scope.startFade = true;
//here i want to use the index to delete the row
$scope.hidden = true;
};
$scope.deleteuser = function (dalId) {
var data = {delId : $scope.delId};
$http.post('delete.php', data )
.success(function(response) {
$scope.hideStuff(delId);
});
};
最佳答案
您可以使用 $index 请阅读以下内容以获取更多信息: https://docs.angularjs.org/api/ng/directive/ngRepeat
我认为你应该这样做:
<tr id="tr-{{item.id}}" ng-repeat="item in ItemsByPage[currentPage]">
<td>
<div>
{{item.id}}
</div>
</td>
<td>
<div editable-text="item.name"
onaftersave='inlineupdateName(item.id,item.name)'
ng-model="tname" data-n='item.name'>
{{item.name}}
</div>
</td>
<td>
<div editable-text="item.phone"
onaftersave='inlineupdatephone(item.id,item.phone)'
ng-model="tphone">
{{item.phone}}
</div>
</td>
</tr>
<input type="text" ng-model="delId" class="form-control"
placeholder="Enter user id to delete th user">
<button ng-click="deleteuser(delId)" type="button"
class="btn btn-primary">
Delete User
</button>
js
$scope.hideStuff = function (delId) {
$("#tr-"+delId).hide();
//the entire tr (line table) will be hidden.
// you don't need those $scope variables to hide the elements
};
$scope.deleteuser = function (dalId) {
var data = {delId : $scope.delId};
$http.post('delete.php', data )
.success(function(response) {
$scope.hideStuff(delId);
});
};
关于javascript - 在 ng-repeat 中获取行的索引(或 id)以对特定行执行一些操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32912591/