数组中特定属性的角度过滤器 [英] Angular filter for specific properties in an array
问题描述
我有一个输入文本的角度过滤器.对于给定的列表
I have an angular filter by input text. for the given list
initialViewModel.users = [
{user: 'Nithin',phone: 'Azus', price: 13000},
{user: 'Saritha',phone: 'MotoG1',price: 12000},
{user: 'Renjith',phone: 'MotoG2',price: 14000},
{user: 'Felix',phone: 'Nexus',price: 21000}];
它为过滤器文本 a,g,m,n,o,s,u,z
返回了错误的结果.
It is returning incorrect results for the filter texts a,g,m,n,o,s,u,z
.
<ul>
<li ng-repeat="user in Model.users | filter: Model.name | orderBy:'price'">{{user.user + ' bought phone worth ' + user.price}}</li>
</ul>
例如,如果我用 'a' 过滤它,它应该只返回一个名为 saritha 的记录.相反,它返回两条记录.
Say for example, if I filter it with 'a' it should return only the one record having name saritha. Instead it returns two records.
推荐答案
默认情况下,根据对象的任何属性进行角度过滤.如果要按特定属性过滤,则需要更新过滤器:
By default angular filters by any property of an object. If you want to filter by specific property you need to update your filter:
<li ng-repeat="user in Model.users | filter: { user: Model.name } | orderBy:'price'">
{{user.user + ' bought phone worth ' + user.price}}
</li>
注意这部分:filter: { user: Model.name }
.在这种情况下,您告诉 angular 仅过滤对象的属性 user
.
Pay attention to this part: filter: { user: Model.name }
. In this case you are telling angular to filter only property user
of your object.
这是一个更新的JSFiddle
这篇关于数组中特定属性的角度过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!