Just trying to learn Angular 2+ (8 specifically) and for the life of me, I can't understand why a class variable is 'undefined' inside a class function, but accessible if I write the function with ES6 style.
I tried setting in constructor instead, but that doesn't make sense.
export class GameControlComponent implements OnInit {
myVar;
counter = 0;
constructor() {} ngOnInit() {}
handleClickStart() {
this.myVar = setInterval(this.myFunc, 1500);
}
myFunc() {
this.counter++;
console.log(this.counter);
}
}
Once 'handleClickStart' is called, this outputs NaN every 1.5 seconds. Why???? I would have expected 1 2 3....
Implementing handleClickStart this way gives me the desired results:
handleClickStart() {
this.myVar = setInterval(() => {
console.log(this.counter + 1);
this.counter++;
}, 1500);
}
But still can't figure out why it didn't work out the first way.