-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);     
  }
R. Richards
  • 24,603
  • 10
  • 64
  • 64
theCoder
  • 731
  • 3
  • 11
  • 19
  • 1
    Possible duplicate of [Javascript/jQuery - "Cannot call method 'push' of undefined" while it IS defined](https://stackoverflow.com/questions/11882650/javascript-jquery-cannot-call-method-push-of-undefined-while-it-is-defined) – Jota.Toledo Feb 08 '19 at 11:25

1 Answers1

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.

Helvetios
  • 76
  • 8
  • 1
    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 – molamk Feb 08 '19 at 11:36