*ngFor with ngModel and value

Asked

Viewed 63 times

0

I have the following repeat structure:

TS

  resposta: string[];

HTML:

<ion-card *ngFor="let pergunta of perguntas; let i = index">

I have an ngModel that I need to print its value at its value:

<ion-textarea name="resposta" [(ngModel)]="resposta[i]" value="{{resposta}}" placeholder="Resposta" clearInput></ion-textarea>

But I get:

Cannot read Property '0' of Undefined

If I do it: [(ngModel)] = "answer" all goes well, but my other elements also receive the value I put in this field.

  • 1

    tries to put a *ngIf="response" on the component. Since the component is started empty it does not have the 0 position of the array. <ion-textarea *ngf="resposta" name="resposta" [(ngModel)]="resposta[i]" value="{{resposta}}" placeholder="Resposta" clearInput></ion-textarea>

  • But it needs to be initialized, even if empty, as it is an input field...

  • 2

    Try initializing it with: resposta: string[] = [""];

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.