Problem with ngModel at Angular 2

Asked

Viewed 618 times

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 is 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 it this way: [ngModel]="model?.name but then nothing returns.

1 answer

1


You are receiving this error because your component does not have a "model" property. Start the object with all properties to avoid 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();

}

Browser other questions tagged

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