0

I have the parent user model like below:

user.ts

   export class User {
           employeeModel: employee = new Employee();
   } 

and I have a child model named employee.ts

   export class Employeee {
           name: string ;
           age: string ;
           position: string ;
   } 

In my component.ts I instantiate the parent model like:

model: User = new User();

And in my template:

<input type="text"  [(ngModel)]="model.employeeModel.name">

Using that codes I get : Cannot read property 'name' of undefined.

What is wrong with my codes?

1
  • hey are you there?? Commented Mar 18, 2017 at 20:03

1 Answer 1

1

Your user object should be of below type

 export class User {
           employee: Employeee 
   } 

   export class Employeee {
           name: string ;
           age: string ;
           position: string ;
   } 

You have instantiated only the model and not the sub properties.

model: User = new User();

So you should be doing this

declaration

model : User = { };

Instancitating

this.model.employee ={
    name: '',
    age: '',
    position: -1
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.