Angular 具有迭代条件的ngForOf和ngIf
我有以下标记Angular 具有迭代条件的ngForOf和ngIf,angular,Angular,我有以下标记 <template ngFor let-childTag [ngForOf]="marketsTag?.children" let-i="index"> <div *ngIf="indexStartDropDownMore >= i" class="markets-filter__tab" [ngClass]="{ 'markets-filter__tab--active': i === selectedTab }" (click)="men
<template ngFor let-childTag [ngForOf]="marketsTag?.children" let-i="index">
<div
*ngIf="indexStartDropDownMore >= i" class="markets-filter__tab"
[ngClass]="{ 'markets-filter__tab--active': i === selectedTab }"
(click)="menuToggle(i, childTag.marketTagId)"
(appMoreDropdown)="childTagLogic($event, i, marketsTag?.children.length)">
{{ childTag?.name }}
</div>
</template>
{{childTag?.name}
值
indexStartDropDownMore
通过方法childTagLogic
中的事件appMoreDropdown
(在迭代元素的OnInit上激发)在每次迭代(事件OnInit)期间动态更改。条件*ngif
不起作用,但[hidden]=“IndexStartDropDown”更有意义-你对*ngif
和[hidden]
使用相同的条件,得到不同的结果?这两个条件都接受布尔值-那么人们如何能做出不同的行为?你能解释一下这一点吗[ngForOf]=“marketsTag?”孩子们“
-您正在迭代哪种类型的结构?布尔条件不适用于*ngIf-它不总是显示内容。”marketsTag?.children'是数组。如果我删除ngif并显示一些类似于{{indexStartDropDownMore>i}}的条件,则每次迭代的某处显示为true,某处显示为false(结束)。应该考虑到indexStartDropDownMor的值在迭代过程中会发生变化,如果我尝试类似于*ngif=“5索引startdropdownmore
中。您可以尝试查看它得到的值,比如在类log(val){console.log(val)}
中创建一个fn,并显示这些值{log(indexStartDropDownMore)}
我看到了以下顺序“欠定义”,数字“13”12次,然后控制台显示数字“11”13次,再次显示数字“11”13次。序列本身由13个元素组成