Home > Net >  ERROR TypeError: Cannot read properties of undefined (reading 'push') in child element in
ERROR TypeError: Cannot read properties of undefined (reading 'push') in child element in

Time:07-05

I have this array in my child element

  @Input() listAnswer: any;

  changestyle(event)
  {
   let activeSpan = event.target;
   this.listAnswer.push(activeSpan.innerText.trim());
  }

passing this variable from parent component

<app-child [listAnswer]="listAnswer"></app-child>

but getting this error

ERROR TypeError: Cannot read properties of undefined (reading 'push')

the same code was working on main component.

Any suggestion Thanks

CodePudding user response:

You should initialized some value during the listAnswer input declaration on your component.ts. Like below:

  @Input() listAnswer: any[]=[];

CodePudding user response:

your listanswer needs to be declared as an array:

@Input listAnswer: any[];

and in your parent component also needs to have its listAnswer property be an array type.

  • Related