Dylan Anlezark
Dylan Anlezark

Reputation: 1267

Angular 2 Nested *ngFor

I am currently trying to pass some indexes to a function but the first parameter (ii) returns 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>

Upvotes: 0

Views: 871

Answers (2)

Michael Kang
Michael Kang

Reputation: 52837

Check out the docs for ngFor. The correct syntax for binding to index:

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

Upvotes: 0

Sajeetharan
Sajeetharan

Reputation: 222522

You should assign index value to the variables not the otherway,

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

also

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

Upvotes: 1

Related Questions