首页 > 解决方案 > Angular 2 嵌套 *ngFor

问题描述

我目前正在尝试将一些索引传递给函数,但第一个参数 (ii) 返回未定义。

<div *ngFor="let tab of screen.data.tabs; let index = i;">
    <div *ngIf="tab.active">
        <div *ngIf="tab.questions">
            <div *ngFor="let question of tab.questions; let index = ii;">
                <div class="scenarioContainerQUESTION">
                    <p [innerHtml]="question.text"></p>
                    <div *ngFor="let option of question.options; let iii = index;" class="option" [ngClass]="{'optionSelected': option.selected}">
                        <label [for]="ii+'_'+iii">{{option.text}}</label>
                        <input [id]="ii+'_'+iii" [name]="'group'+ii" type="radio" [value]="ii" (click)="optionClicked(ii,iii)" />
                    </div>
                    <button [ngClass]="{'fade': selectedOption == -1}" (click)="ManageSubmit()">SUBMIT</button>
                </div>
            </div>
        </div>
    </div>
</div>

标签: htmlangulartypescriptngfor

解决方案


您应该将索引值分配给变量,而不是相反,

<div *ngFor="let tab of screen.data.tabs; let i= index;">

<div *ngFor="let question of tab.questions; let ii= index;">

推荐阅读