过滤一个数组angularjs数组

时间:2014-09-11 10:53:38

标签: javascript arrays angularjs

我有一个客户数组。每个客户都有一个Field对象数组。我想 过滤客户数组,以便我获得具有某些特定字段的所有客户。客户数组看起来就是这样。

[{"id":236,
 "username":"Tsakos Ntinidis",
 "email":"taskos.a@gmail.com",
 "fields":[{"id":2,"name":"Entrepreneurship"},
                {"id":16,"name":"Sciences (Math, Physics, Chemistry etc)"}, 
                {"id":3,"name":"Engineering"},
                {"id":18,"name":"Journalism"}],
                "hasVideo":false,
                "profileDescription":null,"isNew":false},
 {"id":237,
 "username":"Nick Brown",
 "email":"nikk@gmail.com",
 "fields":[{"id":1,"name":"IT"},
                {"id":16,"name":"Sciences (Math, Physics, Chemistry etc)"}, 
                {"id":18,"name":"Journalism"}],
                "hasVideo":false,
                "profileDescription":null}]

和填充字段选择字段的列表是:

[{"id":1,"name":"Finance"},{"id":2,"name":"Entrepreneurship"},{"id":3,"name":"Engineering"},{"id":4,"name":"Economics"}]

客户表的html是这样的:

 <select  ng-model="search.fields.field.id" ng-options="field.id as field.name for field in fields">
</select>

 <tr ng-repeat="customer in customers | orderBy:predicate:reverse | filter:search" ng-     controller="Controller">
                    <td class="id">{{customer.id}}</a></td>
                    <td>{{customer.userName}}</a></td>
 </tr>

我的主要问题是我过滤后数组是空的,所以显然出现了问题。 谢谢你的帮助

1 个答案:

答案 0 :(得分:1)

请看这里http://jsbin.com/cozasamusinu/1/edit 将选择更改为

<select  ng-model="search.fields.id" ng-options="field.id as field.name for field in fields">
</select>

并在你的转发器中

<tr ng-repeat="customer in customers |filter :{fields:search.fields.id}:true">

刚刚在你的字段数组中{a> { id:1 name:Finance}{user id:1 name:IT}