Connect to global events by utilizing host binding, as outlined in the API documentation. You can find more information about this in the DirectiveMetadata page:
@Component({
selector: 'my-app',
template: `<p>Resize window and observe console log.
<p>{{a}} {{b}}`
})
export class AppComponent {
@HostListener('window:resize') onResize() {
this.a++;
this.b++;
console.log(this.a, this.b, event)
}
}
plunker
Your original code fails because you overwrote the onresize
handler (which Angular had also modified) with your own function. As a result, Angular loses the ability to perform change detection after the event handler completes. By using host binding, you ensure that the Angular monkey-patch remains intact, preventing the disabling of change detection so that your view updates seamlessly after the resize event.