Show/Hide 多个 Div 取决于屏幕 angular 2

Show/Hide Multiple Divs depend upon screen angular 2

我想使用一些按钮来 show/hide 多个 divs 使用 angular2

该页面最初将显示所有 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>

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

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;
  }

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

<div *ngIf*="windowWidth > 800">content</div>