【问题标题】:show a div when click an input button单击输入按钮时显示 div
【发布时间】:2020-03-03 20:35:47
【问题描述】:

当我点击另一个组件中的输入按钮时,我想在一个组件中显示一个 div。

例如:

在添加组件中,我有一个输入:<input #openDiv>,当我单击或聚焦它以显示此组件时,我需要:

<div-component [focus]="openDiv"></div-component>

这是我的div-component

export class divComponent implements OnInit , AfterViewInit {

  @Input() focus: ElementRef;
}

我该如何解决这个问题?

【问题讨论】:

    标签: javascript angular typescript


    【解决方案1】:

    试试:

    &lt;div-component *ngIf="showDiv"&gt;&lt;/div-component&gt;

    和:

    &lt;input #openDiv (focus)="showDiv = true" (click)="showDiv = true" /&gt;

    工作堆栈闪电战:https://stackblitz.com/edit/angular-s2p2je

    注意,您也可以使用(focusout)="showDiv = false" 在失去焦点时隐藏 div。 *ngIf 进一步从 DOM 中删除了该元素。如果您想将元素保留在 DOM 中,请使用 [hidden]="!showDiv"

    【讨论】:

    • 我想在那个组件上处理它。我怎样才能将该输入传递给组件并找到该输入的状态?
    猜你喜欢
    • 2021-12-03
    • 2013-01-16
    • 2016-05-19
    • 1970-01-01
    • 1970-01-01
    • 2016-09-17
    • 2014-05-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多