Angular 2 Nested * ngFor

时间:2018-05-03 02:05:11

标签: html angular typescript ngfor

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

<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>

2 个答案:

答案 0 :(得分:1)

您应该为变量指定索引值而不是其他,

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

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

答案 1 :(得分:0)

查看ngFor的文档。绑定到index的正确语法:

<li *ngFor="let item of items; index as i; trackBy: trackByFn">...</li>