我有一个产品数组,我在使用ng-repeat和正在使用
<div ng-repeat="product in products | filter:by_colour">
按颜色过滤这些产品。滤镜正在工作,但如果产品名称/描述等包含颜色,那么在滤镜应用后,产品仍然存在。
我如何设置过滤器只适用于我的数组的颜色字段,而不是每个字段?
我有一个产品数组,我在使用ng-repeat和正在使用
<div ng-repeat="product in products | filter:by_colour">
按颜色过滤这些产品。滤镜正在工作,但如果产品名称/描述等包含颜色,那么在滤镜应用后,产品仍然存在。
我如何设置过滤器只适用于我的数组的颜色字段,而不是每个字段?
当前回答
我的方式是这样
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是Input字段或者其他你喜欢的
像这样显示
<div ng-if="x.id === sub" ng-repeat=" x in subjcts">{{x.title}}</div>
其他回答
你必须使用 过滤器:{color_name:by_colour}代替
filter:by_colour
如果你想匹配一个对象的单个属性,那么写这个属性而不是object,否则其他的属性会被匹配。
我的方式是这样
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是Input字段或者其他你喜欢的
像这样显示
<div ng-if="x.id === sub" ng-repeat=" x in subjcts">{{x.title}}</div>
如果希望筛选给定对象的孙辈(或更深层),则可以继续构建对象层次结构。例如,如果你想过滤'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 } }">
按颜色搜索:
<input type="text" ng-model="searchinput">
<div ng-repeat="product in products | filter:{color:searchinput}">
你也可以做一个内巢。
filter:{prop1:{innerprop1:searchinput}}
小心使用角滤镜。如果你想在字段中选择特定的值,你不能使用过滤器。
例子:
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的东西,这意味着你想选择产品,其中“颜色”包含字符串“蓝色”,而不是“颜色”是“蓝色”。