如何使用ng-repeat中的条件并显示在ng-options中删除重复项的数组组合

时间:2016-08-04 12:22:13

标签: javascript jquery arrays angularjs

我有以下数组:

owner_users = [{username : "sam", name:"Sampath", email:"xyz" },
{username : "ram", name:"Ram Mohan", email:"asd" },
{username : "shyam", name:"Shyam pandey", email:"wer" }]

admin_users = [{username : "sam", name:"Sampath", email:"xyz" },
{username : "pandey", name:"Mangal Pandey", email:"yuy" },
{username : "ameer", name:"Gajini", email:"tyrt" },
{username : "shyam", name:"Shyam pandey", email:"wer" }]

test_users = [{username : "kiran", name:"Kiran", email:"kiran" },
{username : "pandey", name:"Mangal Pandey", email:"yuy" },
{username : "balu", name:"Balakrishna", email:"balu.krsh" },
{username : "shyam", name:"Shyam pandey", email:"wer" }] 

我正在表格中重复执行project_details。 project_details数组具有对象属性 - project_type,owner_name,comment。对于owner_name,我必须在ng-options中列出这种用户类型的组合。就像project_type是new,然后我必须显示owner_user和test_users的组合列表。如果project_type是旧的,那么我必须显示owner_users和admin_users的组合列表,没有重复项。

如何在angularJS中实现这一目标?

1 个答案:

答案 0 :(得分:0)

首先,我使用concat函数为每个可能的project_type组合数组。

例如:old_project_users = owner_users.concat(admin_users);

要删除重复项,我使用unique filter from AngularUI(此处提供的源代码:AngularUI唯一过滤器)并直接在ng-options中使用它。

为了根据条件显示用户列表,我使用ng-if在ng-repeat中选择标记保存在div中。

前:

 <div ng-if="project_type ='new'">
    <select ng-model="project.owner" ng-options="user.username for user in new_project_users| unique:'username'">       
    </select>
    </div>
<div ng-if="project_type ='old'">
    <select ng-model="project.owner" ng-options="user.username for user in old_project_users| unique:'username'">       
    </select>
    </div>