简体   繁体   English

如何在 *ngFor 中获得 *ngIf 的第一个真实值

[英]How to get 1st true value for *ngIf inside *ngFor

I have an array of items that need to be displayed based on roles.我有一系列需要根据角色显示的项目。 I need the first value which will fulfil the ngIf condition.我需要满足 ngIf 条件的第一个值。

I am adding my code below:我在下面添加我的代码:

My Array(kind of how it will originally look):我的数组(它最初的样子):

parentTabList = [ 
        {
            name: 'abc',
            label: 'abc',
            icon : 'question_answer',
            role : ['vend_perm','vend_temp','vend_subs']
        },
        {
            name: 'xyz',
            label: 'xyz',
            icon : 'question_answer',
            role : ['vend_perm','vend_subs']
        }
    ]

My Html: -我的 Html:-

<ng-container *ngFor="let form of parentTabList let i = index">
    <li *ngIf="form.role.includes(userRole)">
        <a (click)="methodName(form)">
            {{form.label}}
        </a>
    </li>
</ng-container>

UserRole is a string value that I get when a user logs-in. UserRole 是我在用户登录时获得的字符串值。

I need to add a ngClass to the anchor tag if it is the first anchor to be displayed.如果它是要显示的第一个锚,我需要将 ngClass 添加到锚标记。 (I am a noob at StackOverflow, please let me know if any more explanation is required). (我是 StackOverflow 的菜鸟,如果需要更多解释,请告诉我)。

You can write like this.你可以这样写。 In this code, f represents the first position of your array.在此代码中,f 代表阵列的第一个 position。

<ng-container *ngFor="let form of parentTabList; let i = index; let f = first">
    <li *ngIf="f">
        <a (click)="methodName(f)">
            `{{f.label}}`
        </a>
    </li>
</ng-container>

If you want other position of your array, you can write like you mentioned above.如果你想要你的阵列的其他 position,你可以像上面提到的那样写。

You can identify first element of the array with index.您可以使用索引标识数组的第一个元素。

But as per my understanding you need filter this array with roles and then apply ngClass to first element from filtered list.但根据我的理解,您需要使用角色过滤此数组,然后将ngClass应用于过滤列表中的第一个元素。

So add method to return filtered array with respect to roles所以添加方法来返回关于角色的过滤数组

In Template:在模板中:

 filterParentTabList(parentList: any) {
     return parentList.filter(form => form.role.includes(this.userRole));
 }

In View:在视图中:

<ng-container *ngFor="let form of filterParentTabList(parentTabList); let i = index">
    <li>
        <a [ngClass]="{ 'addYourClaaName': i === 0 }" (click)="methodName(form)">
            {{form.label}}
        </a>
    </li>
</ng-container>

Happy Coding.. :)快乐编码.. :)

You can define a getter that will get you the index.您可以定义一个获取索引的 getter。 This can then be used in the html然后可以在 html 中使用它

  get firstIndex() {
    return this.parentTabList.indexOf(this.parentTabList.find(({role}) => 
      role.includes(this.userRole)))
  }

Now in your html现在在您的 html

<ng-container *ngFor="let form of parentTabList let i = index">
    <li *ngIf="form.role.includes(userRole)">
        <a [ngClass]="{redText: firstIndex === i}" (click)="methodName(form)">
            {{form.label}}
        </a>
    </li>
</ng-container>

See Stackblitz Demo Here 在此处查看 Stackblitz 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM