1

I am using a angularjs filter method on an repeated array of items and trying to filter numbers with a limitTo filter. The result is not getting applied to the repeat in the DOM.

Here is the html

<div ng-controller="demo as d">
      <input type="text" ng-model="d.test" ng-change="d.filterthis(d.test)"><br>
      <div ng-repeat="item in d.items | limitTo:d.limitto  track by $index">
            <span ng-show="!item.show">{{item.myno}}</span> - 
            <span ng-show="!item.show">{{$index}} - {{item.mystr}}</span><br>
      </div>
</div>

App.js filter function withing angularjs

this.filterthis = function(filter){
    that.items.map(function(filter){

            return function(obj){
                obj.show=true;
                if(obj.myno.toString().indexOf(filter) >-1){
                    console.log(obj);
                    obj.show=false;
                }
                return obj;
            }
    }(filter));
};

Items is a array like this

this.items = [{
        show:false,
        myno:10,
        mystr:"test1"
    }];

http://plnkr.co/edit/bTdlTpSeZuPyGpolXLEG

4

1 回答 1

1

将“显示”作为项目的键,不会将其从 中删除ng-repeat,因此limitTo过滤器仍将返回项目。相反,您应该filter像这样在重复中利用过滤器

<input type="text" ng-model="d.search"><br>
<div ng-repeat="item in d.items | filter:{myno:d.search} | limitTo:d.limitto track by $index">
    <span>{{item.myno}}</span> - 
    <span>{{$index}} - {{item.mystr}}</span><br>
</div>

请注意,顺序在这里很重要,如果您limitTofilter过滤有限的结果。您可以通过更改{myno:d.search}为不同的键来更改您过滤的键,或者如果您想搜索整个对象,只需使用d.search.

更新的 Plunker

于 2017-01-14T04:04:26.963 回答