2015-10-08 166 views
0

已保存!看下面如何。通过输入和自定义功能的角度过滤器

我想用自定义函数和输入搜索来筛选角度。

我的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'实际过滤了大部分客人之后,也能够执行搜索。 如何用功能和输入实现滤波器?

已久! 我通过更改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> 

和我修改了控制器,I取出过滤器是如下:

.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) 

所有的项目列表(我的例子中的客人),搜索输入和$范围(我需要访问范围以外的变量)。

所有心中已经在这里找到答案:https://stackoverflow.com/a/11997076/2346370

感谢巴斯Slagter,我不知道我怎么也没想到通过另一个说法。

回答

0

您可以将searchInput的值传递到您的filterGuests过滤器以在过滤时使用它。

<md-list-item class="md-3-line" ng-repeat="guest in guestList | filterGuests:searchInput" > 
    <div class="md-list-item-text"> 
     <h3>{{guest.name}}</h3> 
    </div> 
</md-list-item> 

我做了一个小例子:http://jsfiddle.net/basslagter/p7czcssq/1/