如何使用 AngularJS 从 ng-repeat 中的数组中删除对象?
How to remove object from array within ng-repeat with AngularJS?
我有一个数组,其中包含 [{...}, {...}] 之类的对象,我正在使用 ng-repeat 输出这些对象。然后我有一个带有删除功能的删除按钮。
在 AngularJS 中是否有一种简单的方法可以删除它,或许可以使用 $index?或者我需要在每个对象上指定一个 ID 作为 属性?
如果您不应用过滤器来重新排序或过滤数组,您可以这样做:
<div ng-repeat="item in items" ng-click="delete($index)">{{item}}</div>
删除函数:
$scope.items = [...];
$scope.delete = function (index) {
$scope.items.splice(index, 1);
}
另一种没有过滤器问题的方法:(仅限 IE9+)
<div ng-repeat="item in items | orderBy: 'id'" ng-click="delete(item)">{{item}}</div>
删除函数:
$scope.items = [...];
$scope.delete = function (item) {
$scope.items.splice($scope.items.indexOf(item), 1);
}
这是另一个例子,同样使用 Jade:
template.jade:
label All Items
ul.list-group
li.list-group-item(ng-repeat="item in items | orderBy: '_id'")
strong {{item.name}}
a.trash(ng-click='deleteItem(item)')
//a.trash is a bootstrap trash icon, but you don't need to use it.
controller.js:
$scope.deleteItem = function (item) {
$scope.items.splice($scope.items.indexOf(item),1);
}
首先尝试这样做,但是列表在运行时没有实现。
$scope.delete = function (index) {
delete $scope.items[index];
}
然后 Facundo Pedrazzini 上面给出的答案对我来说确实有效。
$scope.delete = function (index) {
$scope.items.splice(index, 1);
}
版本:AngularJS v1.6.4
删除
将集合中的每个元素与给定的属性对象进行比较,
返回一个数组,其中没有所有具有等效 属性 值的元素。
$scope.collection = [
{ id: 1, name: 'foo' },
{ id: 1, name: 'bar' },
{ id: 2, name: 'baz' }
]
<tr ng-repeat="obj in collection | removeWith:{ id: 1 }">
{{ obj.name }}
</tr>
<tr ng-repeat="obj in collection | removeWith:{ id: 1, name: 'foo' }">
{{ obj.name }}
</tr>
在blade.php
<table style="width:100%;">
<tr ng-repeat="name in planFormData.names track by $index">
<td>
<div class="form-group">
<label>Plan Name<span style="color:red;">*</span> </label>
<input type="text" class="form-control" ng-model="planFormData.names[$index].plan_name" name="plan_name" id="status-name" placeholder="Plan Name" autocomplete="off" required>
</div>
</td>
<td>
<a href="JavaScript:Void(0);"><i class="icon-plus" ng-click="addRow($index)" ng-show="$last"></i></a>
<a href="JavaScript:Void(0);"><i class="icon-trash" ng-click="deleteRow($event,name)" ng-show="$index != 0"></i></a>
</td>
</tr>
</table>
在controller.js
$scope.deleteRow = function($event, name) {
var index = $scope.planFormData.names.indexOf(name);
$scope.planFormData.names.splice(index, 1);
};
在Angular6中,我对多维数组做了类似的操作。有效
RemoveThisTimeSlot(i: number, j: number) {
this.service.formData.ConsultationModelInfo.ConsultationWeekList[i].TimeBlockList.splice(j, 1);
}
我有一个数组,其中包含 [{...}, {...}] 之类的对象,我正在使用 ng-repeat 输出这些对象。然后我有一个带有删除功能的删除按钮。
在 AngularJS 中是否有一种简单的方法可以删除它,或许可以使用 $index?或者我需要在每个对象上指定一个 ID 作为 属性?
如果您不应用过滤器来重新排序或过滤数组,您可以这样做:
<div ng-repeat="item in items" ng-click="delete($index)">{{item}}</div>
删除函数:
$scope.items = [...];
$scope.delete = function (index) {
$scope.items.splice(index, 1);
}
另一种没有过滤器问题的方法:(仅限 IE9+)
<div ng-repeat="item in items | orderBy: 'id'" ng-click="delete(item)">{{item}}</div>
删除函数:
$scope.items = [...];
$scope.delete = function (item) {
$scope.items.splice($scope.items.indexOf(item), 1);
}
这是另一个例子,同样使用 Jade:
template.jade:
label All Items
ul.list-group
li.list-group-item(ng-repeat="item in items | orderBy: '_id'")
strong {{item.name}}
a.trash(ng-click='deleteItem(item)')
//a.trash is a bootstrap trash icon, but you don't need to use it.
controller.js:
$scope.deleteItem = function (item) {
$scope.items.splice($scope.items.indexOf(item),1);
}
首先尝试这样做,但是列表在运行时没有实现。
$scope.delete = function (index) {
delete $scope.items[index];
}
然后 Facundo Pedrazzini 上面给出的答案对我来说确实有效。
$scope.delete = function (index) {
$scope.items.splice(index, 1);
}
版本:AngularJS v1.6.4
删除 将集合中的每个元素与给定的属性对象进行比较, 返回一个数组,其中没有所有具有等效 属性 值的元素。
$scope.collection = [
{ id: 1, name: 'foo' },
{ id: 1, name: 'bar' },
{ id: 2, name: 'baz' }
]
<tr ng-repeat="obj in collection | removeWith:{ id: 1 }">
{{ obj.name }}
</tr>
<tr ng-repeat="obj in collection | removeWith:{ id: 1, name: 'foo' }">
{{ obj.name }}
</tr>
在blade.php
<table style="width:100%;">
<tr ng-repeat="name in planFormData.names track by $index">
<td>
<div class="form-group">
<label>Plan Name<span style="color:red;">*</span> </label>
<input type="text" class="form-control" ng-model="planFormData.names[$index].plan_name" name="plan_name" id="status-name" placeholder="Plan Name" autocomplete="off" required>
</div>
</td>
<td>
<a href="JavaScript:Void(0);"><i class="icon-plus" ng-click="addRow($index)" ng-show="$last"></i></a>
<a href="JavaScript:Void(0);"><i class="icon-trash" ng-click="deleteRow($event,name)" ng-show="$index != 0"></i></a>
</td>
</tr>
</table>
在controller.js
$scope.deleteRow = function($event, name) {
var index = $scope.planFormData.names.indexOf(name);
$scope.planFormData.names.splice(index, 1);
};
在Angular6中,我对多维数组做了类似的操作。有效
RemoveThisTimeSlot(i: number, j: number) {
this.service.formData.ConsultationModelInfo.ConsultationWeekList[i].TimeBlockList.splice(j, 1);
}