在ng-repeat中,如何使用if条件,并显示在ng选项中移除重复的数组的组合?

时间:2022-04-03 20:35:27

I have the following arrays:

我有如下的数组:

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

and

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" }]

and

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" }] 

I am making ng-repeat of project_details in a table. project_details array is with object properties -- project_type, owner_name, comment. for owner_name i have to list this combination of user types in ng-options. Like if project_type is new then i have to show combined list of owner_user and test_users. if project_type is old, then i have to show combined list of owner_users and admin_users without duplicates.

我正在一个表中对project_details进行ng重复。project_details数组具有对象属性——project_type、owner_name、comment。对于owner_name,我必须列出ng选项中用户类型的组合。如果project_type是新的,那么我必须显示owner_user和test_users的组合列表。如果project_type是旧的,那么我必须显示owner_users和admin_users的合并列表,而不需要重复。

How can I achieve this in angularJS?

我怎样才能在angularJS中实现这一点呢?

1 个解决方案

#1


0  

First, I used concat function to make the arrays combined for each of the possible project_type.

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

Ex: old_project_users = owner_users.concat(admin_users);

例:old_project_users = owner_users.concat(admin_users);

To remove duplicates, i used the unique filter from AngularUI (source code available here: AngularUI unique filter) and use it directly in the ng-options.

为了删除重复,我使用了来自AngularUI(这里可用的源代码:AngularUI unique filter)的唯一过滤器,并直接在ng选项中使用它。

To show users list based on conditions, i used ng-if inside ng-repeat for select tag keeping inside a div.

为了显示基于条件的用户列表,我使用了ng-如果在ng-repeat中选择保持在div中的标签。

Ex:

例:

 <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>

#1


0  

First, I used concat function to make the arrays combined for each of the possible project_type.

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

Ex: old_project_users = owner_users.concat(admin_users);

例:old_project_users = owner_users.concat(admin_users);

To remove duplicates, i used the unique filter from AngularUI (source code available here: AngularUI unique filter) and use it directly in the ng-options.

为了删除重复,我使用了来自AngularUI(这里可用的源代码:AngularUI unique filter)的唯一过滤器,并直接在ng选项中使用它。

To show users list based on conditions, i used ng-if inside ng-repeat for select tag keeping inside a div.

为了显示基于条件的用户列表,我使用了ng-如果在ng-repeat中选择保持在div中的标签。

Ex:

例:

 <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>