Angular 4 loading events using @HostListener

This is what I am trying to do:

import { Directive, HostListener, Input } from '@angular/core';

@Directive({
selector: '[appResizeWindow]'
})
export class ResizeWindowDirective {
@Input('appResizeWindow') line_ChartOptions: {};

constructor() { }

@HostListener('window:resize', ['$event']) onResize(event: Event) {
console.log('Yo');
if (event.target['innerWidth'] < 420)
  this.line_ChartOptions['hAxis']['format'] = 'MMM';
else if (event.target['innerWidth'] < 760)
  this.line_ChartOptions['hAxis']['format'] = 'MM. yy\'';
else this.line_ChartOptions['hAxis']['format'] = 'MMM d, yyyy';
}

@HostListener('load', ['$event']) onPageLoad(event: Event) {
   console.log('loaded');
   this.onResize(event.target['innerWidth']);
  }
}

So, "window.resize" works fine when I attack in a template.

The problem is loading . I tried the event onload

I want the page to execute when the page loads.

What am I missing here?

+4
source share
1 answer

The event loadhas already occurred before the initialization of your component / directive.

ngAfterViewInit() / , , .

+1

Source: https://habr.com/ru/post/1681561/


All Articles