Update: I tried adding this to the beginning of authToken() and the event emits just fine, so there is something happening inside of the subscribe event that is not allowing the event to emit?
@Output() tokenAcquired: EventEmitter = new EventEmitter();
...
authToken(authCode: string) {
this.tokenAcquired.emit("testing123");
...
I have written a component that emits a string in my subscribe to an observable provided by a service. Testing from another component with an emit without an observable shows that the parent component and HTML are receiving events just fine. It;'s just that then I attempt to emit from within my observable that I get no event emitting.
It seems that I cannot fire an emit inside of the subscribe event. Where is the proper place an emit based on data obtained by a subscription to an observable?
@Output() tokenAcquired: EventEmitter = new EventEmitter();
...
authToken(authCode: string) {
this.authservice.spotifyGetToken(authCode).subscribe( data => {
this.token = <AccessToken>data;
console.log("Token Raw: " + this.token.access_token);
this.tokenAcquired.emit("testing123");
},
(err: HttpErrorResponse) => {
console.log(err.message + ' : ' + err.status);
});
HTML
<app-auth #auth
[code] = "authCode"
(tokenAcquired)="receiveToken($event);">
</app-auth>
Parent Componenet
receiveToken($event) {
console.log("receiveToken event fired!" + $event)
this.token = $event
localStorage.setItem('token', $event);
console.log("Saved token object to local storage...");
}
Not sure if this helps, but running in a debugger. After the event SHOULD have emitted, here is the status of the EventEmitter: Event Emitter after firing
(tokenAcquired)="receiveToken($event)"
– Eldho<app-test (testEvent)="receiveToken($event);"> </app-test>
works. – Ahackthis.authChild.authToken(this.authCode);
I guess this created a NEW instance of the child class? By changing the child function to use local vars and not class variables, the event does fire now. So it had something to do with calling the function from component code vs from the child HTML button. It works now, although I'm not clear on why. – Ahack