angularjs - 过滤以从表 AngularJS 中删除一些数据
问题描述
这是我的代码,我不知道为什么它不起作用:
JS:
$scope.names = [{"name" : "John_Sparrow"},
{"name" : "Jack_Black"},
{"name" : "Eva_Sparrow"}]
app.filter('FilterFunction',function(){
return function(data, input) {
if (!input) return data;
var datas = [];
angular.forEach(data, function(item){
if(!(item.name.includes('Sparrow'))) {
datas.push(item);
}
});
return datas;
};
});
HTML:
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Name</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="name in names |FilterFunction:input">
<td>{{name.name}}</td>
</tr>
</tbody>
</table>
<button class="btn btn-clear" ng-click="input = !input;">delete_sparrow</button>
单击按钮后,我想删除所有带有“麻雀”的名称。
提前谢谢解答!!!
解决方案
您需要将过滤器放在控制器之外。
演示
var app = angular.module('app', []);
app.controller('HelloWorldCtrl', function($scope){
$scope.names = [{"name" : "John_Sparrow"},
{"name" : "Jack_Black"},
{"name" : "Eva_Sparrow"}];
});
app.filter('FilterFunction',function(){
return function(data, input) {
if (!input) return data;
var datas = [];
angular.forEach(data, function(item){
if(!(item.name.includes('Sparrow'))) {
datas.push(item);
}
});
return datas;
};
});
<!DOCTYPE html>
<html ng-app="app">
<head>
<script data-require="angular.js@*" data-semver="4.0.0" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.10/angular.min.js"></script>
<link rel="stylesheet" href="style.css" />
</head>
<body ng-controller="HelloWorldCtrl">
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Name</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="name in names |FilterFunction:input">
<td>{{name.name}}</td>
</tr>
</tbody>
</table>
<button class="btn btn-clear" ng-click="input = !input;">delete_sparrow</button>
</body>
</html>
推荐阅读
- postgresql - 运行 docker postrgessql image 两次先退出
- javascript - 页面上的多个倒数计时器
- api - 如何在 Jmeter 请求中处理方括号 []
- java - 如何在 java 中将 POI 工作簿添加为 json 元素
- javascript - 在找到的示例代码中,Array.prototype.find 的箭头函数回调中令人惊讶的语法是什么?
- sql - 联合嵌套选择中的排序依据
- configuration - 配置 SonarQube (Node.JS) 以从内部服务器下载二进制文件
- powershell - Powershell 按值传递复杂对象,而不是按引用
- elixir - Elixir - 动态设置 stripity_stripe 库的请求标头
- ios - 在自定义对象类型的核心数据中保存带有子项的类别