【问题标题】:How to change the visibility of a label through typecript如何通过打字稿改变标签的可见性
【发布时间】:2020-03-02 22:36:11
【问题描述】:

我有一个最初隐藏的文本标签。单击按钮时,我想让它可见,但我没有这样做。如果有人可以帮助我,我会很高兴

HTML 代码

<label for="Validi"  class="validi" id="alert" >Invalid Credentials. Please try again</label>

SCSS 代码

.validi {
    color: red;
    font-weight: bold;
    visibility: hidden;
}

我想在单击按钮时通过我的打字稿文件将可见性更改为“可见”。

【问题讨论】:

  • 而不是visibilityshow hide标签的使用条件
  • 您可以在 &lt;label&gt; 元素上使用 [ngClass]angular.io/api/common/NgClass
  • 你也可以使用 *ngIf,因为你已经包含了 angular 标签

标签: html angular typescript


【解决方案1】:

试试这个代码

.html

<label for="Validi" class="validi" id="alert" *ngIf="error">Invalid Credentials. Please try again</label>
<button (click)="showError()">Show Error</label> //OR
<button (click)="error=true">Show Error</label>

.ts

error: boolean = false;

showError(){
  this.error = true;
}

【讨论】:

    【解决方案2】:

    试试这个:

    example.html

    <label for="Validi" *ngIf="show" class="validi" id="alert" >Invalid Credentials. Please try again</label>
    <button type="button" (click)="show = !show">Click me !</button>
    

    example.ts

    public show = true;
    

    example.css

    .validi {
      color: red;
      font-weight: bold;
    }
    

    【讨论】:

    • @BhargavChudasama 你在使用一个函数
    • 见问题On clicking a button I want to make it visible
    • 而不是说toggle label on button click
    猜你喜欢
    • 2020-07-21
    • 1970-01-01
    • 2017-02-22
    • 1970-01-01
    • 1970-01-01
    • 2022-12-01
    • 2021-01-19
    • 1970-01-01
    • 2016-04-10
    相关资源
    最近更新 更多