Error in ngModel call

0

I'm trying a 'Can not read property' reason_social 'of undefined' error when calling a ngModel in my html.

I created an interface like this:

export interface IEmpresas {
  nome_fantasia : string;
  razao_social : string;
  cnpj : string;
}

In my component, I instantiated the interface and created a variable with the type of my interface.

...
private empresa : IEmpresas;
...

and then, I call ngModel in html

<input type="text" [(ngModel)]="empresa.razao_social" id="razao_social">
    
asked by anonymous 26.05.2018 / 17:30

2 answers

0

To start the object with an empty value

private empresa : IEmpresas = {
  nome_fantasia : '';
  razao_social : '';
  cnpj : '';
};
    
26.05.2018 / 22:38
1

The company variable is starting as undefined, or null.

You declare it with the operator safe (company? .razao_social). This should solve, although it is not the ideal solution.

    
26.05.2018 / 19:01