我希望能够使用作为数组传入的许多参数来过滤我的表。因此,我可以建立一个过滤器参数数组并将它们传递进来。我不想明确说明要过滤的列,因为可能有很多列(有些将显示,有些不显示)。
HTML 看起来像这样;
<tr ng-repeat="item in infoData | filter:['param1','param2']">
<td>{{item.data1}}</td>
<td>{{item.data2}}</td>
<td>{{item.data3}}</td>
<td>{{item.data4}}</td>
</tr>
是否可以针对多个参数过滤表?
谢谢
最佳答案
这是完成您需要的快速而肮脏的方式。
首先在 Controller 中创建一个自定义过滤器,如下所示:
$scope.customFilter = function(param1, param2) {
return function(item) {
//return custom stuff here
}
}
然后在 html 中你这样做
<tr ng-repeat="item in infoData | filter:customFilter(param1, param2)">
<td>{{item.data1}}</td>
<td>{{item.data2}}</td>
<td>{{item.data3}}</td>
<td>{{item.data4}}</td>
</tr>
这是一个带有自定义过滤器的示例
app.filter('customFilter', function (param1, param2) {
return function (item) {
//return custom stuff here
};
});
现在在 html 中你这样做:
<tr ng-repeat="item in infoData | customFilter(param1, param2)">
<td>{{item.data1}}</td>
<td>{{item.data2}}</td>
<td>{{item.data3}}</td>
<td>{{item.data4}}</td>
</tr>
关于具有多个参数的 AngularJS 过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25851515/