Recently, I was assigned a task to build a responsive layout; however, I was not allowed to take the help of CSS media query; on the other hand, I was to use the *ngIf directive as the window size changes.
So, I decided to go through the Angular window size detection with the help of the HostListener API.
Sometimes, you might need to perform the dev tasks depending on the specific screen size, it could be anything for instance adding a class, adding styling, or call any function.
In Angular, getting a real-time window size on screen size changing is exorbitantly effortless. In this tutorial, we will learn how to detect the width and height of the screen in Angular using the HostListener decorator.
The HostListener is a Decorator used for listening to the DOM, and It provides a handler method to run when that event occurs.
Angular calls the provided handler method when the host element emits the named event and updates the associated element with the result.
So, let us begin and understand how to get the window height and width in Angular 12 application.
Angular development solely relies on Angular CLI; you make sure you have the required tool installed.
If not, then run the command to install the Angular command-line interface (CLI).
npm install -g @angular/cli
In this step, you have to download the angular application using the given below command.
However, you may utterly skip this step if you have installed the app in advance.
ng new ng-demo
Next, step inside the project directory.
cd ng-demo
Import HostListener API from ‘@angular/core’ package, define variables get screen width and getScreenHeight, use the HostListener to bind window to resize event to get the screen size and width on window resize.
Update the code in src/app/app.component.ts file.
import { Component, HostListener } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
public getScreenWidth: any;
public getScreenHeight: any;
ngOnInit() {
this.getScreenWidth = window.innerWidth;
this.getScreenHeight = window.innerHeight;
}
@HostListener('window:resize', ['$event'])
onWindowResize() {
this.getScreenWidth = window.innerWidth;
this.getScreenHeight = window.innerHeight;
}
}
In this step, you have to open the angular HTML template file and define the variables using the double curly braces to print the screen or window size on the browser.
Please update the code in src/app/app.component.html file.
<div class="container text-center mt-5">
<p>Window width: <strong>{{ getScreenWidth }}</strong></p>
<p>Window height: <strong>{{ getScreenHeight }}</strong></p>
</div>
All the necessary steps have been done, now you have to type the given command and hit enter to run the angular app server.
ng serve
Now, open the browser, type the given url and hit enter and view the screen size on window resize in angular.
http://localhost:4200
In this Angular screen size detect example, we have gone through significant steps and learned how to get responsive screen sizes when the user changes the window size or resize the screen size.
In this post, we will learn how to work with HTTP requests in the Redux…
MySQL is a relational database management system based on SQL – Structured Query Language, and…
React Js Handle Rest API data globally with Context and useState hook tutorial. In this…
Node js delete data from MySQL database tutorial; Throughout this guide, you will ascertain how…
In this tutorial, you will discover how to import CSV file into MySQL database using…
React Js Global state management with createContext hook example; In this tutorial, we will help…