显示/隐藏多个Div取决于屏幕角度2

时间:2017-01-19 15:08:32

标签: html angular typescript ng2-bootstrap

我想使用一些按钮来显示/隐藏使用angular2

的多个div

该页面最初将显示所有div。当小屏幕我隐藏所有div然后分开按钮显示特定div而隐藏其余部分。

非常感谢任何帮助。

<div class="buttons">
<a class="button" id="showdiv1">Div 1</a>
<a class="button" id="showdiv2">Div 2</a>
<a class="button" id="showdiv3">Div 3</a>
</div>


<div class="row">
  <div class="col-xlg-4 col-xl-5 col-lg-12 col-sm-12 col-xs-12 hidden-sm-down">
  </div>

   <div class="col-xlg-4 col-xl-5 col-lg-12 col-sm-12 col-xs-12 hidden-sm-down">
  </div>

   <div class="col-xlg-4 col-xl-5 col-lg-12 col-sm-12 col-xs-12 hidden-sm-down">
  </div>
</div>

1 个答案:

答案 0 :(得分:4)

使用@HostListenerresize事件来确定视口的宽度。然后在组件中设置一个具有此值的属性,然后您可以将其提供给HTML模板中的ngIf*

&#13;
&#13;
import { Component, HostListener, AfterViewInit } from '@angular/core';

@Component()

export class AppComponent { 

  windowWidth: number = window.innerWidth;

  //initial values, The window object may still be undefined during this hook, let me know if that's the case and we'll figure out a better hook for the initial value
  ngAfterViewInit() {
      this.windowWidth = window.innerWidth;
  }

  //if screen size changes it'll update
  @HostListener('window:resize', ['$event'])
  resize(event) {
      this.windowWidth = window.innerWidth;
  }

}
&#13;
<!-- Then any HTML element you wish to toggle apply the *ngIf directive as so: -->

<div *ngIf*="windowWidth > 800">content</div>
&#13;
&#13;
&#13;