Angular2: Unsubscribe from host listener [duplicate]

匿名 (未验证) 提交于 2019-12-03 01:45:01

问题:

This question already has an answer here:

If I subscribe to an event in an Angular2 component using host{}, how can I unsubscribe from the event?

@Component({     selector: "foo",     host: {         "(window:resize)": "doSomething($event)"     } }); 

I've tried to use $event to unsubscribe, but the event itself never presents the scenario in which I want to unsubscribe.

For now, I'm just doing a poor man's short circuit, but I'd prefer to avoid the extra checks involved in short circuiting.

Poor man's short circuit:

private _conditionMet:boolean = false; doSomething(e) {     if(this._conditionMet) {         return;     }     ... }  somethingElse() {     this._conditionMet = true; } 

回答1:

You can't unsubscribe from host-listener. If you need to unsubscribe you need to subscribe imperatively.

See also Programmatically (un)register to event with Angular 2



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!