2016-08-04 72 views
-1

我有以下陣列:如何使用,如果在NG-重複和數組去除NG選項重複的節目組合條件

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的ng-repeat。 project_details數組具有對象屬性 - project_type,owner_name,comment。對於owner_name,我必須在ng選項中列出這些用戶類型的組合。就像如果project_type是新的,那麼我必須顯示owner_user和test_users的組合列表。如果project_type是舊的,那麼我必須顯示owner_users和admin_users的組合列表而不重複。

我如何在angularJS中實現這一點?

+2

你有什麼迄今所做? – Mistalis

+0

兄弟請給更多的信息,否則檢查相關的問題http://stackoverflow.com/questions/23595034/removing-duplicates-from-angular-js-ng-options-ng-repeat http://jsfiddle.net/YJQx6/5 / –

回答

0

首先,我使用concat函數爲每個可能的project_type組合數組。

例:old_project_users = owner_users.concat(admin_users);

要刪除重複,我使用的unique filter from AngularUI(源代碼可在這裏:AngularUI唯一濾波器),並且直接在NG-選項使用它。

爲了顯示基於條件的用戶列表,我使用了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>