user3653474
user3653474

Reputation: 3854

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

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

Upvotes: 0

Views: 844

Answers (2)

Abdus Salam Azad
Abdus Salam Azad

Reputation: 5502

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

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

Upvotes: 1

anthonyb
anthonyb

Reputation: 372

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.

Upvotes: 0

Related Questions