在AngularJs中迭代对象时,如何在ng-repeat中使用filter

时间:2022-11-25 17:28:31

Search field binded to a model

搜索字段绑定到一个模型。

<input type="text" ng-model="searchVoucher" />

An object iterated in ng-repeat

在ng-repeat中迭代的对象

<li ng-repeat="(k,f) in {'r':4,'e':5,'t':6,'y':7,'c':8} | filter:searchVoucher">{{f}}</li>

How can i filter based on the object's key or val or val can also be an object having attributes.

如何基于对象的键或val或val进行筛选也可以是具有属性的对象。

Please help

请帮助

2 个解决方案

#1


2  

Try

试一试

<li ng-repeat="(k,f) in {'r':4,'e':5,'t':6,'y':7,'c':8} | searchfilter:searchVoucher">{{f}}</li>

Filter

过滤器

app.filter('searchfilter', function() {
  return function(input, term) {
    var regex = new RegExp(term || '', 'i');
    var obj = {};
    angular.forEach(input, function(v, i){
      if(regex.test(v + '')){
        obj[i]=v;
      }
    });
    return obj;
  };
});

Demo: Plunker

演示:一美元

#2


1  

Please have a look at this http://jsfiddle.net/fFLUH/. Here the same filter searchVoucher is configured to filter the input based on key or value, based on the filterParam. The keyword this that is passed from li reffers to the controller tst. This tst controller has the model for the textboxes.

请查看这个http://jsfiddle.net/fFLUH/。在这里,同样的过滤搜索凭证被配置为根据过滤器的键值或值来过滤输入。从li reffers传递到控制器tst的关键字。这个tst控制器拥有文本框的模型。

Inside the filter, im accessing the filterParam, and using that to filter the input json.

在过滤器内部,我访问filterParam,并使用它过滤输入json。

#1


2  

Try

试一试

<li ng-repeat="(k,f) in {'r':4,'e':5,'t':6,'y':7,'c':8} | searchfilter:searchVoucher">{{f}}</li>

Filter

过滤器

app.filter('searchfilter', function() {
  return function(input, term) {
    var regex = new RegExp(term || '', 'i');
    var obj = {};
    angular.forEach(input, function(v, i){
      if(regex.test(v + '')){
        obj[i]=v;
      }
    });
    return obj;
  };
});

Demo: Plunker

演示:一美元

#2


1  

Please have a look at this http://jsfiddle.net/fFLUH/. Here the same filter searchVoucher is configured to filter the input based on key or value, based on the filterParam. The keyword this that is passed from li reffers to the controller tst. This tst controller has the model for the textboxes.

请查看这个http://jsfiddle.net/fFLUH/。在这里,同样的过滤搜索凭证被配置为根据过滤器的键值或值来过滤输入。从li reffers传递到控制器tst的关键字。这个tst控制器拥有文本框的模型。

Inside the filter, im accessing the filterParam, and using that to filter the input json.

在过滤器内部,我访问filterParam,并使用它过滤输入json。