I want to create a custom directive which can detect the change in browser height and width.
My Directive:
import { Directive, ElementRef, Input, NgZone} from '@angular/core';
@Directive({ selector: '[resize]' })
export class ResizeDirective {
constructor(private ngZone: NgZone) {
window.onresize = (e) => {
//ngZone.run will help to run change detection
this.ngZone.run(() => {
console.log("Width: " + window.innerWidth);
console.log("Height: " + window.innerHeight);
});
};
}
}
app.module.ts:
import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { ResizeDirective } from './resize.directive';
@NgModule({
imports: [
BrowserModule,
FormsModule,
],
declarations:
[AppComponent, ResizeDirectiv],
bootstrap: [AppComponent],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
I have placed my directive inside the Body tag of the app.component.html. So whenever the browser screen size changes, I need to perform specific operation.
app.component.html
<div class="outterContainer" style="width:100%;height:100%;" resizePlugin>
<div id="header1" class="header">
</div>
<div id="contentSection" class="content">
<div>
<div>Resize Demo</div>
<input [(ngModel)]="studname">{{studname}}
<createProject></createProject>
<AddEvent></AddEvent>
<h2>NavBar + Routing + DragAndDrop + ModalPopup + ReAgrrange Demo</h2>',
</div>
<div>
<div>
<h2>NavBar + Routing + DragAndDrop + ModalPopup Demo</h2>
<navbar></navbar>
<router-outlet></router-outlet>
</div>
</div>
</div>
</div>
Am I doing something wrong? Is there anyway I could modify this directive to make it listen for browser screen size change?