【问题标题】:angular filter by input and custom function通过输入和自定义函数进行角度过滤
【发布时间】: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,我不知道我怎么没想过要传递另一个参数。

【问题讨论】:

    标签: angularjs filter


    【解决方案1】:

    您可以将 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/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-29
      • 1970-01-01
      • 1970-01-01
      • 2022-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-19
      相关资源
      最近更新 更多