Problem with ngModel in Angular 2

0

I'm trying to get the values of 3 inputs that are in a modal of Ionic 2 but it gives this error: Error in ./ModalPage class ModalPage - caused by: Cannot read property 'name' of undefined

My input code looks like this:

<input type="text" [(ngModel)]="model.name" name="name" placeholder="Nome">
<input type="text" [(ngModel)]="model.email" name="email" placeholder="E-mail">
<input type="text" [(ngModel)]="model.phone" name="phone" placeholder="Telefone">

I even tried to do this: [ngModel]="model?.name but there it does not return anything.

    
asked by anonymous 29.12.2016 / 01:49

1 answer

1

You're getting this error because your component does not have a "model" property. Start the object with all the properties to avoid the error:

export class MeuComponente {

  model: Model = {
    name: '',
    email: '',
    phone: ''
  };
}

Or create a class with the model properties and start a new instance:

import { Model } from '/.model'; 

export class MeuComponente {

  model: new Model();

}
    
29.12.2016 / 19:43