【发布时间】:2019-09-21 06:31:30
【问题描述】:
我在 For 循环中有多个卡片。在每张卡片中,我想使用链接或按钮显示和隐藏卡片内的内容。我没有唯一的 ID 来分配我想要切换的每个 div。有没有办法做到这一点?
以下代码仅展开或折叠第一张卡片中的内容,即使单击任何卡片中的任何“展开”或“折叠”按钮也是如此。
<div *ngFor="let result of results">
<div class="clr-col-lg-12 clr-col-12">
<div class="card">
<div class="card-block">
<div *ngIf="result?.name">
<h3 class="card-title">{{result.name}}</h3>
</div>
<div class="expandCollapse">
<button (click)="toggle($event)">
{{buttonName}}
</button>
</div>
<div class="expandCollapseContent" *ngIf="showRuleContent">
<div *ngIf="result?.cTag">
<h5>C Tag</h5>{{result.cTag}}
</div>
</div>
</div>
</div>
</div>
</div>
组件
public showRuleContent:boolean = false;
public buttonName:any = 'Expand';
toggle($event) {
this.showRuleContent = !this.showRuleContent;
// CHANGE THE NAME OF THE BUTTON.
if(this.showRuleContent)
this.buttonName = "Collapse";
else
this.buttonName = "Expand";
}
【问题讨论】:
-
您可以绑定到 [hidden] 。见stackoverflow.com/questions/35163009/…
标签: angular