角度过滤器,用于数组中的特定属性 [英] 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.
这篇关于角度过滤器,用于数组中的特定属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!