ng-repeat:按单个字段过滤

时间:2013-02-06 15:52:09

标签: angularjs ng-repeat angularjs-filter

我有一系列产品,我正在重复使用ng-repeat并使用

<div ng-repeat="product in products | filter:by_colour"> 

按颜色过滤这些产品。过滤器正在运行,但如果产品名称/描述等包含颜色,则在应用过滤器后产品仍然存在。

如何将滤镜设置为仅应用于数组的颜色字段而不是每个字段?

12 个答案:

答案 0 :(得分:568)

指定要应用过滤器的属性(即colour):

<div ng-repeat="product in products | filter:{ colour: by_colour }">

答案 1 :(得分:463)

请参阅filter页面上的示例。使用对象,并在color属性中设置颜色:

Search by color: <input type="text" ng-model="search.color">
<div ng-repeat="product in products | filter:search"> 

答案 2 :(得分:171)

您可以使用与您必须对其进行过滤的对象匹配的属性进行过滤:

app.controller('FooCtrl', function($scope) {
   $scope.products = [
       { id: 1, name: 'test', color: 'red' },
       { id: 2, name: 'bob', color: 'blue' }
       /*... etc... */
   ];
});
<div ng-repeat="product in products | filter: { color: 'red' }"> 

这可以通过变量传递,正如马克拉杰克建议的那样。

答案 3 :(得分:105)

如果要过滤给定对象的孙(或更深),可以继续构建对象层次结构。例如,如果要对'thing.properties.title'进行过滤,则可以执行以下操作:

<div ng-repeat="thing in things | filter: { properties: { title: title_filter } }">

您还可以通过将对象添加到过滤器对象来过滤对象的多个属性:

<div ng-repeat="thing in things | filter: { properties: { title: title_filter, id: id_filter } }">

答案 4 :(得分:97)

最好的方法是使用一个函数:

<强> HTML

<div ng-repeat="product in products | filter: myFilter">

<强>的javascript

$scope.myFilter = function (item) { 
    return item === 'red' || item === 'blue'; 
};

或者,您可以使用ngHide或ngShow根据特定条件动态显示和隐藏元素。

答案 5 :(得分:63)

小心角度滤镜。如果您想在字段中选择特定值,则无法使用过滤器。

示例:

<强>的javascript

app.controller('FooCtrl', function($scope) {
   $scope.products = [
       { id: 1, name: 'test', color: 'lightblue' },
       { id: 2, name: 'bob', color: 'blue' }
       /*... etc... */
   ];
});

<强> HTML

<div ng-repeat="product in products | filter: { color: 'blue' }"> 

这将选择两者,因为使用像substr这样的东西。这意味着你想要选择产品,其中&#34;颜色&#34;包含字符串&#34; blue&#34;而不是在哪里&#34;颜色&#34;是&#34;蓝&#34;。

答案 6 :(得分:18)

按颜色搜索:

<input type="text" ng-model="searchinput">
<div ng-repeat="product in products | filter:{color:searchinput}">

你也可以做内巢。

filter:{prop1:{innerprop1:searchinput}}

答案 7 :(得分:9)

如果您要执行以下操作:

<li class="active-item" ng-repeat="item in mc.pageData.items | filter: { itemTypeId: 2, itemStatus: 1 } | orderBy : 'listIndex'"
                id="{{item.id}}">
    <span class="item-title">{{preference.itemTitle}}</span>
</li>

...你不仅会获得itemTypeId 2和itemStatus 1的项目,而且还会获得itemType为20,22,202,123和itemStatus 10,11,101,123的项目。这是因为过滤器: {...}语法就像包含查询的字符串一样。

但是,如果您要添加:true 条件,则会按完全匹配进行过滤:

<li class="active-item" ng-repeat="item in mc.pageData.items | filter: { itemTypeId: 2, itemStatus: 1 } : true | orderBy : 'listIndex'"
                id="{{item.id}}">
    <span class="item-title">{{preference.itemTitle}}</span>
</li>

答案 8 :(得分:4)

我的方式就是这个

subjcts is

[{"id":"1","title":"GFATM"},{"id":"2","title":"Court Case"},{"id":"3","title":"Renewal\/Validity"},{"id":"4","title":"Change of Details"},{"id":"5","title":"Student Query"},{"id":"6","title":"Complains"}]
  

sub是输入字段或您喜欢的任何内容

像这样显示

<div ng-if="x.id === sub" ng-repeat=" x in subjcts">{{x.title}}</div>

答案 9 :(得分:4)

你必须使用  filter:{color_name:by_colour}代替

filter:by_colour

如果要与对象的单个属性匹配,则写入该属性而不是对象,否则其他一些属性将匹配。

答案 10 :(得分:1)

在过滤器中指定要应用过滤器的对象的属性:

//Suppose Object
var users = [{
  "firstname": "XYZ",
  "lastname": "ABC",
  "Address": "HOUSE NO-1, Example Street, Example Town"
},
{
  "firstname": "QWE",
  "lastname": "YUIKJH",
  "Address": "HOUSE NO-11, Example Street1, Example Town1"
}]

但您只想在名字

上应用过滤器
<input type = "text" ng-model = "first_name_model"/>
<div ng-repeat="user in users| filter:{ firstname: first_name_model}">

答案 11 :(得分:0)

如果要过滤一个字段:

label>Any: <input ng-model="search.color"></label> <br>
<tr ng-repeat="friendObj in friends | filter:search:strict">

如果要过滤所有字段:

 label>Any: <input ng-model="search.$"></label> <br>
 <tr ng-repeat="friendObj in friends | filter:search:strict">

https://docs.angularjs.org/api/ng/filter/filter对您有好处

相关问题