I found this solution. Is it valid?
import {Component} from '@angular/core'; import {Platform} from 'ionic-angular'; @Component({...}) export MyApp { constructor(platform: Platform) { platform.ready().then((readySource) => { console.log('Width: ' + platform.width()); console.log('Height: ' + platform.height()); }); } }
This solution is for ionic 2. can i also used for angular 2?
Please suggest me the right way to do it.
Try using window. innerHeight and window. innerWidth to get height and width of the device respectively.
To get the width and height of the window in React:Use the innerWidth and innerHeight properties on the window object. Add an event listener for the resize event in the useEffect hook. Keep changes to the width and height of the window in a state variable.
For those who want to get height and width of device even when the display is resized (dynamically & in real-time):
In that Component do: import { HostListener } from "@angular/core";
In the component's class body write:
@HostListener('window:resize', ['$event']) onResize(event?) { this.screenHeight = window.innerHeight; this.screenWidth = window.innerWidth; }
In the component's constructor
call the onResize
method to initialize the variables. Also, don't forget to declare them first.
constructor() { this.onResize(); }
Complete code:
import { Component, OnInit } from "@angular/core"; import { HostListener } from "@angular/core"; @Component({ selector: "app-login", templateUrl: './login.component.html', styleUrls: ['./login.component.css'] }) export class FooComponent implements OnInit { screenHeight: number; screenWidth: number; constructor() { this.getScreenSize(); } @HostListener('window:resize', ['$event']) getScreenSize(event?) { this.screenHeight = window.innerHeight; this.screenWidth = window.innerWidth; console.log(this.screenHeight, this.screenWidth); } }
I found the solution. The answer is very simple. write the below code in your constructor.
import { Component, OnInit, OnDestroy, Input } from "@angular/core"; // Import this, and write at the top of your .ts file import { HostListener } from "@angular/core"; @Component({ selector: "app-login", templateUrl: './login.component.html', styleUrls: ['./login.component.css'] }) export class LoginComponent implements OnInit, OnDestroy { // Declare height and width variables scrHeight:any; scrWidth:any; @HostListener('window:resize', ['$event']) getScreenSize(event?) { this.scrHeight = window.innerHeight; this.scrWidth = window.innerWidth; console.log(this.scrHeight, this.scrWidth); } // Constructor constructor() { this.getScreenSize(); } }
====== Working Code (Another) ======
export class Dashboard { mobHeight: any; mobWidth: any; constructor(private router:Router, private http: Http){ this.mobHeight = (window.screen.height) + "px"; this.mobWidth = (window.screen.width) + "px"; console.log(this.mobHeight); console.log(this.mobWidth) } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With