How to Get Width and Height of Screen in Angular?
Hi Dev,
This tutorial will provide example of angular get height and width of screen. i explained simply about how to get screen width in angular. if you have question about how to get screen height in angular then i will give simple example with solution. In this article, we will implement a get screen height in angular.
we will see simple example of how to detect window height and width in angular 6, angular 7, angular 8, angular 9, angular 10, angular 11, angular 12, angular 13, angular 14, angular 15, angular 16 and angular 17 application. you can also see example of getting window size on resize event in angular.
I will give you two example that will help you getting window size in angular app.
Example 1:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'my-app',
template: `
<p>Screen width: {{ screenWidth }}</p>
<p>Screen height: {{ screenHeight }}</p>
`,
styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit {
name = 'Angular';
public screenWidth: any;
public screenHeight: any;
ngOnInit() {
this.screenWidth = window.innerWidth;
this.screenHeight = window.innerHeight;
}
}
Output:
Screen width: 1535
Screen height: 762
Example 2: Detect Window Size on Resize in Angular
import { Component, OnInit, HostListener } from '@angular/core';
@Component({
selector: 'my-app',
template: `
<p>Screen width: {{ screenWidth }}</p>
<p>Screen height: {{ screenHeight }}</p>
`,
styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit {
name = 'Angular';
public screenWidth: any;
public screenHeight: any;
ngOnInit() {
this.screenWidth = window.innerWidth;
this.screenHeight = window.innerHeight;
}
@HostListener('window:resize', ['$event'])
onResize(event) {
this.screenWidth = window.innerWidth;
this.screenHeight = window.innerHeight;
}
}
Output:
Screen width: 960
Screen height: 752
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Angular Material Tooltip Example
- How to Use Interface in Angular?
- Angular Json Pipe Example | Json Pipe in Angular
- Angular NgIf Example | NgIf Directive In Angular
- 10 Digit Mobile Number Validation in Angular
- File Upload with Angular Reactive Forms Example
- Angular Define Global Variables Tutorial
- How to Install And Use JQuery in Angular?
- Angular Toggle a Div on Button Click Example
- How to Get Current URL in Angular?