2017-02-14 11 views
0

ブール値の値をウィンドウのサイズに基づいて変更しようとしていますが、構文に問題があります。ここで角度2のif/elseステートメントでブール値を変更します。

は、私が持っているものです:

export class AppComponent { 

     isSmall = true; 

     constructor(){ 
     let windowWidth = window.innerWidth; 

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

すべてのヘルプは、スーパーいただければ幸いです!

+0

この「トラブル」とはなんですか?エラーが報告されていますか? – Pointy

+0

また、 '<= '比較はその値としてブール値を持つので、単に' isSmall = windowWidth <= 700; 'と書くことができます。 – Pointy

答えて

1
export class AppComponent { 

isSmall:boolean = true; 
windowWidth: number; 

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

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

あなたはコードの下の角度で窓幅を取りたい場合は...

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

は今、あなたは値が上の更新を取得するには、あなたの変数

0

を設定することができます$ window.innerWidthを使用するのに役立ちますウィンドウのサイズ変更、使用することができます

@HostListener('window:resize') 
onResize() { 
    this.isSmall = windowWidth <= 700; 
} 
関連する問題