2015-10-08 164 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/