Bird Dad
Bird Dad

Reputation: 427

Angular 4/5 ngFor unexpected index error

I am trying to make a list of inputs and I can't get ngFor to compile.

<div *ngFor="let q of questions; let i = index" class="col-3">
  <div class="group">
    <input [(ngModel)]="q" [class.ng-not-empty]="q.length > 0">
    <span class="bar"></span>
    <label>Question {{i}}</label>
  </div>
</div>

my definitions are set as such:

questions: string[];
constructor() {
  this.questions = ['blah', 'blah', 'blah'];
}

the error i get is:

ERROR in : Error: Cannot assign to a reference or variable!

If I remove the index definition it works and i have tried all of the following:

*ngfor="let q of questions | index as i"
*ngfor="let q of questions; index as i"

Upvotes: 0

Views: 731

Answers (1)

filipbarak
filipbarak

Reputation: 1905

You can't use [(ngModel)]="q" because you are trying to assign to a local variable in your HTML (in this case, let q)

Try this:

[(ngModel)]="questions[i]";

Upvotes: 6

Related Questions