I have an error with [(ngModel)] in an ionic 2 application

0

I have an error and I do not know what is wrong My code is as follows:

Html

<ion-col col-12 class="informacion flex" *ngFor=" let edad of edades; let i=index ">
    <ion-input class="" [(ngModel)]="edad" type="text" placeholder="Ej: (7-12) Infantil, Etc..." ></ion-input>
    <button class="papelera" ion-button color="rojoTomato" clear icon-only (click)="eliminarEdad(i)">
        <ion-icon name="ios-trash-outline" ></ion-icon>
    </button>
</ion-col>

ts

edades = [''];

Error

  

Error: Uncaught (in promise): Error: Can not assign to a reference or   variable! Error: Can not assign to a reference or variable!

Other examples that work:

Html

<ion-col col-12 class="flex" *ngFor=" let valor of valores; let i=index ">
    <ion-input class="" [(ngModel)]="valor[0]" type="text" placeholder="Ej: Infantil" ></ion-input>
    <ion-input class="" [(ngModel)]="valor[1]" type="text" placeholder=" $20.000 COP." ></ion-input>
    <button ion-button class="papelera" color="rojoTomatoColsport" clear icon-only (click)="eliminarValor(i)">
        <ion-icon name="ios-trash-outline" ></ion-icon>
    </button>
</ion-col>

ts

valores= [['','']];
    
asked by Daniel Enrique Rodriguez Caste 19.09.2017 в 05:22
source

1 answer

2

The error is saying that you can not assign to [(ngModel)] a variable / reference created locally for the input value dynamically.

One solution would be to refer to the object edades to avoid the failure:

[(ngModel)]="edades[i]"

But you lose the focus (focus) of the input, because you are repeating over a matrix and you are changing the elements of the matrix.

    
answered by 19.09.2017 / 12:52
source