-2

I am trying to push a value into an array but I am getting error:

Cannot read property 'push' of undefined.

My html code is:

<ion-item *ngFor="let item of items"  (click)="clicked(item.title)">
   {{item.title}}
   </ion-item>
</ion-list>

And my ts code is:

  clicked(item){

    this.addedtags.push(item);
    console.log(this.addedtags);     
  }
1

1 Answer 1

1

Your addedtags array is undefined. Initialize the array at the top of your class.

addedtags: any[] = []

Replace any with your specific datatype for cleaner code.

Sign up to request clarification or add additional context in comments.

1 Comment

Good answer. I think it's good to mention that the piece you wrote needs to be at the top of the class and not inside the function clicked(item) or it will be overwritten at each call

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.