angular - 在 Angular 中,我们如何在 ngFor 循环中定义的 ngIf 中使用局部变量?
问题描述
我是 Angular 新手,需要帮助来实现动态垂直选项卡:
我正在使用我在 ngIf 条件的 ngFor 循环中定义的变量 i 。我不知道如何在 ngIf 条件下使用变量 i 。我尝试了下面的代码,但它似乎不起作用。
<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代码:
showtabcontent(id) {
debugger;
id = 'tab-content-' + id;
// this.currTab = 'tab-content-0';
this.currTab = id;
alert(id);
}
解决方案
in*ngIf
i+3
被认为是一个字符串。在进行数学运算之前,您必须关闭单引号。
*ngIf="currTab == 'tab-content-' + (3 + i)"
完整示例:
<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>
推荐阅读
- javascript - 弹性搜索查询不适用于非保留字符
- java - 从 preorder 和 inorder 创建 BinaryTree 时出现 java.lang.NullPointerException
- ios - 如何从本地 storekit 配置文件验证服务器上的收据数据?
- python - Django 删除函数覆盖 pgtrigger
- python - 从用python爬行的网站编写csv
- python - 无法获取 Instagram 页面
- angularjs - ng-repeat 之外的 AngularJS 访问循环长度
- r - 向不稳定的文本添加图标
- python - Python中具有n个值的列表
- prometheus - 如果 metricX 在任何时间点的百分比下降超过 70% 并持续 5 分钟,则生成警报