html - Angular4+ 在 HTML 模板中显示/隐藏子元素

标签 html angular typescript

我正在尝试在我的 Angular 应用程序中实现一个简单的“悬停在评论上以显示回复按钮”的实现。有没有办法通过仅使用模板引用变量来实现这种效果?

有点像……

<mat-list>
  <mat-list-item *ngFor="let comment of comments" #SomeRefToThisParticularElement (mouseenter)="SomeRefToThisParticularElement=true" (mouseleave)="SomeRefToThisParticularElement=false">
    <h4>{{comment.text}}</h4>
    <p> 3 replies </p>
    <mat-icon *ngIf="SomeRefToThisParticularElement==true">reply</mat-icon>
  </mat-list-item>
</mat-list>

显然,上面的方法不起作用,因为 Angular 不允许您按照我在上面的代码片段中显示的方式分配或设置模板变量。但我想探索在 html 模板级别实现此目的的选项。

这是执行此操作的好方法吗?

编辑 点击here获取可能解决方案的摘要。

最佳答案

试试这个,根据数组的悬停索引隐藏和显示。

<mat-list>
    <mat-list-item *ngFor="let comment of comments; let i = index" (mouseenter)="commentIndex= i" (mouseleave)="commentIndex = -1">
        <h4 mat-line>{{comment.text}}</h4>
        <p mat-line style="font-size:x-small; color:rgba(0, 0, 0, 0.54)"> 3 replies </p>
        <mat-icon *ngIf="commentIndex == i">reply</mat-icon>
    </mat-list-item>
</mat-list>

关于html - Angular4+ 在 HTML 模板中显示/隐藏子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49233616/

相关文章:

javascript - Firebase 在加载 View 之前未接收数据 - 在填充之前返回空数组

css - 如何在 Angular 7 中同时使用 css 和 sass 文件?

typescript - 为什么在 typescript 中实现接口(interface)时必须重新声明类型

typescript - REST - 强类型模型

jquery - 在 HTML 中,单击时将 unicode 字符 ☰ 替换为 X

javascript - 我对使用 JavaScript 数组感到困惑

javascript - 如何在没有滚动条(实际滚动)的情况下增加滚动上的数字?

javascript - 在 Angular 上,当我们试图从自定义服务返回一些数据时,为什么我们需要一个额外的返回语句

没有 ngIf 的 Angular 动画

javascript - 如何从文本文件中提取数据并输出到网页表格