user7560726
user7560726

Reputation:

Change boolean with if / else statement in angular 2

I am trying to change the value on a boolean based on the size of the window but am having trouble with the syntax.

Here is what I've got:

    export class AppComponent {

      isSmall = true;

      constructor(){
        let windowWidth = window.innerWidth;

        if (windowWidth <= 700) {
            isSmall = true;
        }
        else {
           isSmall = false;
        }
      }
    }

Any help would be super appreciated!

Upvotes: 1

Views: 1452

Answers (3)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 658017

To get the value updated on window resize, you can use

@HostListener('window:resize')
onResize() {
    this.isSmall = windowWidth <= 700;
}

Upvotes: 0

flashjpr
flashjpr

Reputation: 470

export class AppComponent {

isSmall:boolean = true;
windowWidth: number;

constructor(){
    this.windowWidth = window.innerWidth;

    if (windowWidth <= 700) {
       this.isSmall = true;
    }
    else {
       this.isSmall = false;
    }
  }
}

Upvotes: 2

Jayanti Lal
Jayanti Lal

Reputation: 1185

if you want to take windows width in angular below code will help..

  angular.element($window).bind('resize', function () {
    alert($window.innerWidth);
   });

now using $window.innerWidth you can set your variable

Upvotes: 0

Related Questions