【发布时间】:2016-01-05 17:43:56
【问题描述】:
被宠坏了!看看下面的方法。
我正在尝试使用自定义函数和输入搜索来过滤角度。
我的 HTML 看起来像:
<md-list-item class="md-3-line" ng-repeat="guest in guestList | filter:filterGuests && searchInput" >
<div class="md-list-item-text">
<h3>{{guest.name}}</h3>
</div>
</md-list-item>
其中 'searchInput' 是基本输入标签,而函数 'filterGuests' 看起来像:
$scope.filterGuests = function(guest){
if($scope.$parent.filters[guest.status] == true)
return true;
return false;
我正在尝试做的事情是在“filterGuest”实际上过滤了大部分 guest 之后,以便还能够执行搜索。 如何通过函数和输入实现过滤器?
被宠坏了! 我通过更改 HTML 和控制器来实现这一点。 在 HTML 中,我将其更改为:
<md-list-item class="md-3-line" ng-repeat="guest in guestList | filterGuests:searchInput:this" >
<div class="md-list-item-text">
<h3>{{guest.name}}</h3>
</div>
</md-list-item>
我修改了控制器,我取出过滤器为:
.filter('filterGuests',function($log){
return function(guests,input,scope){
var result = [];
angular.forEach(guests,function(guest,key){
if(scope.$parent.filters[guest.status] == true && guest.name.search(input) > -1)
result.push(guest);
});
return result;
};
});
参数传入过滤器为:
function(guests,input,scope)
所有项目列表(在我的例子中是客人),搜索的输入和 $scope(我需要访问范围之外的变量)。
我在这里找到的所有答案:https://stackoverflow.com/a/11997076/2346370
感谢 Bas Slagter,我不知道我怎么没想过要传递另一个参数。
【问题讨论】: