I have the following angular2 component, where I implemented a listenerSelectedChanges()
on an EventEmitter
.
I would like to unsubscribe from the events, when needed.
For that purpose the idea was - to use a function instead of a lambda.
Now I faced the problem, that in my listenerSelectedChanges()
function - the ".this" object becomes invalid.
Question: What is the lifecycle of a function in JS / Typescript? Can the function live longer, than its parent?
export class Auth0Component implements OnInit, OnChanges {
@Input() selectedEnvironment: Environment;
@Output() identityTokenReceived = new EventEmitter(); // component should use the emitter of the AWSService
listenerSelectedChanges = function (idToken: string) {
this.identityTokenReceived.emit(idToken);
};
// bind to the AWSservice emitter
ngOnChanges(changes: SimpleChanges) {
console.log(changes);
if (this.selectedEnvironment !== undefined) {
// would like to use listenerSelectedChanges() here
this.selectedEnvironment.auth0Session.identityTokenReceived.subscribe(idToken => {
this.identityTokenReceived.emit(idToken);
});
}
// unsubscribe here
}
}