【发布时间】:2018-09-20 13:12:50
【问题描述】:
我对使用 Angular 非常陌生,并且遇到了一个问题!我在 Angular 5 中制作的常见问题解答页面上创建了一个点击事件。我遇到的问题是,当我单击一个常见问题解答时,它们都会打开,而不仅仅是目标问题。
这是我的ts:
public show_content = true;
showContent () {
this.show_content = !this.show_content;
}
还有我的html:
<h4 class="accordion-toggle" (click)="showContent()">When is the app ready for download?</h4>
<hr>
<div class="accordion-content" *ngIf="!show_content">
<p>
Cras malesuada ultrices augue molestie risus.
</p>
</div>
<h4 class="accordion-toggle" (click)="showContent()">When is the app ready for download?</h4>
<hr>
<div class="accordion-content" *ngIf="!show_content">
<p>
Cras malesuada ultrices augue molestie risus.
</p>
</div>
我知道我在两者上都使用了相同的 *ngIf,这就是发生这种情况的原因。我要问的问题是:有没有办法在不为每个常见问题解答创建多个变量的情况下做到这一点?我知道创建一个像这样的新函数:
showContent2 () {
this.show_content2 = !this.show_content2;
}
然后在我的 html 中有这个:
<h4 class="accordion-toggle" (click)="showContent2()">When is the app ready for download?</h4>
<hr>
<div class="accordion-content" *ngIf="!show_content2">
这确实可以解决问题,但是我想知道是否有更好的方法可以做到这一点,因为这似乎很冗长。
【问题讨论】:
标签: javascript html typescript angular5