I am porting some code from an AngularJS component into an Angular 5 component.
I have an array of objects loaded into a variable productlist.
In my old controller, I created a second variable as an empty array, showcaselist.
I run a forEach loop on the productlist to find all items that meet a condition (item.acf.product_slide.length > 0) and push them into the showcaselist. I then display these items in my template.
Logging to console shows the data is coming in, and the if statement works, but I keep getting a console error:
TypeError: undefined is not an object (evaluating 'this.showcaselist')
Here is the whole component:
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
@Component({
selector: 'pb-ds-showcaseindex',
templateUrl: './showcaseindex.component.html'
})
export class ShowcaseindexComponent implements OnInit {
productlist;
showcaselist = [];
constructor(private _route: ActivatedRoute) { }
ngOnInit() {
this.productlist = this._route.snapshot.data.showcases;
this.itemsWithSlides();
}
itemsWithSlides = function () {
this.productlist.forEach(function (item) {
if (item.acf.product_slide.length > 0) {
this.showcaselist.push(item);
}
});
};
}
itemsWithSlides = function....is not correct syntax.