Similar to this question but I already seem to have the selected answer. I have the following...
import {Component, OnDestroy} from '@angular/core';
import {MdIconRegistry} from "@angular2-material/icon/icon-registry";
import {MD_ICON_DIRECTIVES} from "@angular2-material/icon/icon";
import {ShowcaseCardComponent} from "./showcase-card/showcase-card.component";
import { WheelService } from "../../wheel/wheel.service";
@Component({
selector: 'jg-showcase',
template: String(require('./showcase.component.pug')),
styles: [String(require('./showcase.component.styl'))],
directives: [MD_ICON_DIRECTIVES, ShowcaseCardComponent],
viewProviders: [MdIconRegistry]
})
export class ShowcaseComponent implements OnDestroy{
currentCard: number = 0;
subscription: any;
cards = [
{
number: 0,
color: "blue",
content: "<h1>Card1</h1>"
},
{
number: 1,
content: "<h1>Card2</h1>"
}
];
constructor(wheelService: WheelService){
this.subscription = wheelService.wheelEmitter.subscribe((data: any) => {
if(data.direction > 0 && this.currentCard !== this.cards.length){
this.currentCard++;
}
else if(this.currentCard){
this.currentCard--;
}
console.log("Current Card "+this.currentCard);
})
};
ngOnDestroy() {
this.subscription.dispose();
};
}
problem is that this
is undefined in the inspector but I see the proper console.log...
Current Card 1
Current Card 2
Why is the console saying it is undefined
this.currentCard
VM10154:1 Uncaught TypeError: Cannot read property 'currentCard' of undefined(…)