简体   繁体   English

带有 OR 条件的 ng-repeat 过滤器

[英]ng-repeat filter with OR condition of property

I wanted to filter a list with a model value from the text box.我想从文本框中过滤一个带有模型值的列表。

Example例子

 var person={};
 person.Id=1;
 person.Name="Test 1";
 person.PetName="Rest 1"

 var persons=[];
 persons.push(person);

 person.Id=2;
 person.Name="Test ds";
 person.PetName="Rest sd";
 persons.push(person);

Persons array will have multiple persons. Persons 数组将有多个人。 In HTML I have one search box.在 HTML 中,我有一个搜索框。

<input type="text" ng-model="seachText" placeholder="search by Name and pet name"/>

<div ng-repeat="person in persons | filter : {$:seachText}">
<div>{{person.Name}}</div>
</div>

Case 1:情况1:

When I enter 2 in the text box, one result will appear.当我在文本框中输入 2 时,会出现一个结果。 I don't want that.我不想要那个。 I want to filter by name and pet-name only.我只想按名称和宠物名称过滤。

Is there any possibility to achieve this without a custom filter?如果没有自定义过滤器,是否有可能实现这一目标? Does filter directive allow OR condition for properties?过滤指令是否允许属性的 OR 条件?

Please help.请帮忙。 Thanks.谢谢。

For me custom "OR" filter is preferred solution, nevertheless you can use built-in filter two times(for each OR condition) and then UNION ALL results:对我来说, 自定义“OR”过滤器是首选解决方案,但您可以使用内置filter两次(对于每个 OR 条件),然后使用 UNION ALL 结果:

 angular.module('app', []).controller('ctrl', function($scope){ $scope.persons = [ {Id:95, Name:"Tom", PetName: "Pet1"}, {Id:96, Name:"Sam", PetName: "Pet2"}, {Id:97, Name:"Max", PetName: "Pet3"}, {Id:98, Name:"Henry", PetName: "Pet4"} ] })
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app='app' ng-controller='ctrl'> <input type='text' ng-model='search'/> <div ng-repeat='person in temp = (persons | filter : {Name: search})'> {{person | json}} </div> <div ng-repeat='person in persons | filter : {PetName: search}' ng-if='temp.indexOf(person) == -1'> {{person | json}} </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM