如何删除数组中使用过滤器的对象?AngularJS
how to delete object in an array with filter used? AngularJS
我有一个类似输入的东西,可以将项目添加到第1列或第2列中,每次添加项目时,该列都会立即显示添加的内容,旁边会有一个"X",所以如果您想删除该项目,只需单击"X"即可。一开始我没有想太多,所以我用了一种简单的方法来删除HTML,但后来我意识到,这只是删除HTML(如果我在搜索中键入一些内容并清除搜索,还有一个搜索输入,所有项目都会再次显示)。这是当我意识到仅仅删除HTML是一个错误时,我也需要删除对象,但我如何才能使它删除数组中正确的对象?
我的角度脚本
angular.module("addItemApp", [])
.controller("toDoCtrl", function ($scope) {
$scope.items = [];
$scope.addItem = function (item) {
console.log(item);
$scope.items.push(angular.copy(item));
console.log($scope.items);
};
$scope.remove = function (item) {
var index = $scope.items.indexOf(item);
$scope.items.splice(index, 1);
}
});
我的html
<div class="row">
<div class="col-xs-6 col-sm-4 left-column">
<div class="input-item">
<input type="text" placeholder="Enter Item" ng-model="item.name" class="enter-item">
<select class="column-select" ng-model="item.pos">
<option value="default" selected>Choose Column</option>
<option value="column1">Column 1</option>
<option value="column2">Column 2</option>
</select>
<button class="add-button" type="button" ng-click="addItem(item)">Add Item</button>
</div>
<div class="search-item">
<label for="search">Search An Item</label>
<div class="search-input">
<input ng-model="query" type="text" placeholder="Search" id="search"><span class="fa fa-search icon-search"></span>
</div>
</div>
</div>
<div class="col-xs-6 col-sm-4">
<h3 class="column-header column1">
Column 1
</h3>
<ul ng-repeat="item in items | filter:{ pos: 'column1' } | filter:query">
<li>{{item.name}}
<button remove-on-click ng-click="remove()" class="remove-button fa fa-times"></button>
</li>
</ul>
</div>
<!-- Optional: clear the XS cols if their content doesn't match in height -->
<div class="clearfix visible-xs-block"></div>
<div class="col-xs-6 col-sm-4">
<h3 class="column-header column2">
Column 2
</h3>
<ul ng-repeat="item in items | filter:{ pos: 'column2' } | filter:query">
<li>{{item.name}}
<button remove-on-click ng-click="remove()" class="remove-button fa fa-times"></button>
</li>
</ul>
</div>
</div>
提前感谢大家。
您可以通过两种方式来实现这一点-
1
$scope.remove = function(item) {
var index = $scope.items.indexOf(item);
$scope.items.splice(index, 1);
}
<button ng-click="remove(item)"></button>
2
$scope.remove = function(index) {
$scope.items.splice(index, 1);
}
<button ng-click="remove($index)"></button>
请注意,当应用筛选器时,$index可能不是您应该用来删除的索引,最好使用第一种方法。我举了$index的例子供你们参考。
<button ng-click="remove(item)"></button>
应该可以工作,因为item
是在ng-repeat
的前面定义的,并且您已经在$scope
上定义了remove
函数。
相关文章:
- 在Angular.JS-ng隐藏vs过滤器中将对象分隔为两组
- jQuery过滤器,返回不同的jQuery对象(即$(this).Pparent())
- 键上的javascript数组对象过滤器
- 如何对对象的多个属性使用角度过滤器
- 如何将Underscore.js过滤器与对象一起使用
- 如何遍历对象数组并将值放入过滤器中
- 对象中的角度过滤器空数组
- 通过过滤器数组设置对象数组的属性
- 使用 NG 重复上的对象作为过滤器
- 主干集合过滤器与位置和获取对象数组
- ng-repeat中的Angular Js对象过滤器
- AngularJS,过滤器:如何将一个巨大的对象(JSON)变成一个数组
- 对象中嵌套数组上的角度ng重复过滤器
- 使用类似查询的对象过滤器数组下划线
- 如何在 JavaScript 中向 SVG 对象添加过滤器
- 聚合物获取过滤器返回的对象的属性
- 如何在jquery中对对象动态添加多个过滤器
- OpenLayers比较过滤器/对象属性
- 使用纯JavaScript,给定一个对象集合和一个过滤器对象,返回集合中与过滤器对象具有相同键/值对的对象
- Angular JS - 基于使用相同ng模型的多个对象属性的ng-repeat过滤器对象数组