【问题标题】:Angular 2+ Close div with x buttonAngular 2+用x按钮关闭div
【发布时间】:2018-09-13 16:53:56
【问题描述】:

有没有办法用 x 按钮关闭 div?在这种情况下,div 是一个“通知”,它会出现几秒钟然后消失。我不想使用隐藏,因为显然 div 永远不会出现。

【问题讨论】:

标签: html css angular


【解决方案1】:

您可以使用 *ngIf 结构指令根据布尔值呈现 div。您需要做的是更改按钮单击时的布尔值。

app.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  public showNotification: boolean;

  constructor() {
    this.showNotification = true;
    setInterval(() => {
      this.showNotification = true;
    }, 3000);
  }

  public onCloseClick(): void {
    this.showNotification = false;
  }
}

app.component.html

<div>
  <div class="notification" *ngIf="showNotification">
    <div class="close" (click)="onCloseClick()">x</div>
    <div class="content">
        Some content
    </div>
  </div>
</div>

我假设 showNotification 变量是从服务或其他东西更新的。这就是为什么我使用 setTimeInterval 来更新 showNotifications 变量的值。

【讨论】:

  • X 按钮是什么意思.. html 上的按钮或键盘 X 按钮按下事件?
  • 我猜是 HTML [X] 按钮什么的
  • 添加了代码示例
  • 对不起,我很忙。我的意思是按钮内的 x。现在我会试试你的解决方案
【解决方案2】:

也许你可以在你的 html 标签中使用布尔值和 *ngIf ?因此每次你想切换它。

这是一个小例子

<button type="button" (click)="visible = false" >x</button>
<div *ngIf="visible">
<!-- rest of your html tags here -->
</div>

你的组件:

export class YourComponent{
 visible:boolean = true;
}

【讨论】:

  • 这不是答案,这是评论
  • 我无法发表评论,因为我没有足够的声誉,网站通常将其视为评论,但这次不是..
  • 是的,当您没有足够的代表时,您还不能发布 cmets,只能发布答案。现在你的答案看起来不错,所以请点赞:)
猜你喜欢
  • 1970-01-01
  • 2013-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-15
  • 2013-09-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多