我有一个产品数组,我在使用ng-repeat和正在使用

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

按颜色过滤这些产品。滤镜正在工作,但如果产品名称/描述等包含颜色,那么在滤镜应用后,产品仍然存在。

我如何设置过滤器只适用于我的数组的颜色字段,而不是每个字段?


当前回答

如果希望筛选给定对象的孙辈(或更深层),则可以继续构建对象层次结构。例如,如果你想过滤'thing.properties. 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 } }">

其他回答

如果你想过滤一个字段:

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对你有好处

如果希望筛选给定对象的孙辈(或更深层),则可以继续构建对象层次结构。例如,如果你想过滤'thing.properties. 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 } }">

小心使用角滤镜。如果你想在字段中选择特定的值,你不能使用过滤器。

例子:

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的东西,这意味着你想选择产品,其中“颜色”包含字符串“蓝色”,而不是“颜色”是“蓝色”。

指定你想要应用滤镜的属性(即颜色):

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

你必须使用 过滤器:{color_name:by_colour}代替

filter:by_colour

如果你想匹配一个对象的单个属性,那么写这个属性而不是object,否则其他的属性会被匹配。