【问题标题】:Show/Hide Multiple Divs depend upon screen angular 2显示/隐藏多个 div 取决于屏幕角度 2
【发布时间】:2017-01-19 15:08:32
【问题描述】:

我想使用一些按钮来使用 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>

【问题讨论】:

  • 您使用 Angular 2 自己尝试过什么?您发布的代码只是 HTML。

标签: html angular typescript ng2-bootstrap


【解决方案1】:

使用@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>

【讨论】:

  • 非常感谢。如何显示特定的 div 点击事件
  • 像这样:&lt;div (click)="myFunction()"&gt; 然后在你的组件中定义myFunction()
猜你喜欢
  • 1970-01-01
  • 2014-09-18
  • 1970-01-01
  • 1970-01-01
  • 2012-04-04
  • 2019-08-17
  • 2023-03-27
  • 1970-01-01
  • 2012-09-30
相关资源
最近更新 更多