Reputation:
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
Reputation: 658017
To get the value updated on window resize, you can use
@HostListener('window:resize')
onResize() {
this.isSmall = windowWidth <= 700;
}
Upvotes: 0
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
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