简体   繁体   English

在 angular 中,我们如何在 ngFor 循环中定义的 ngIf 中使用局部变量?

[英]In angular how can we use local variable in ngIf that has been defined in the ngFor loop?

I am new to angular and need the help in implementing the dynamic vertical tabs:我是 angular 的新手,需要帮助来实现动态垂直选项卡:

I am using the variable i that I defined in the ngFor loop in the ngIf condition.我正在使用我在 ngIf 条件的 ngFor 循环中定义的变量 i 。 I don't know how to use the variable i in the ngIf condition.我不知道如何在 ngIf 条件下使用变量 i 。 I tried the below code but it doesn't seem to work.我尝试了下面的代码,但它似乎不起作用。

 <div
          class="pb-5"
          *ngFor="let studentBatch of studentBatchList; let i = index"
        >
          <div
            id="{{ 'tab-content-' + (3 + i) }}"
            *ngIf="currTab == 'tab-content-(3+i)'"
          >
            {{ i + 3 }} <br />
            {{ studentBatch.batchId }}
          </div>
        </div>

TS code: TS代码:

showtabcontent(id) {
debugger;
id = 'tab-content-' + id;
// this.currTab = 'tab-content-0';
this.currTab = id;
alert(id);

} }

In the *ngIf i+3 is considered as a string.*ngIf i+3中被认为是一个字符串。 You must close the single quote before making mathematical operation.在进行数学运算之前,您必须关闭单引号。

*ngIf="currTab == 'tab-content-' + (3 + i)"

Complete example:完整示例:

<div class="pb-5" *ngFor="let studentBatch of studentBatchList; let i = index">
    <div id="{{ 'tab-content-' + (3 + i) }}" *ngIf="currTab == 'tab-content-' + (3 + i)">
        {{ i + 3 }} <br />
        {{ studentBatch.batchId }}
    </div>
</div>

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

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