![](/img/trans.png)
[英]In Angular 9 how to allow only one toggle in div to be active at a given time?
[英]How to select only one div at a time in angular 10
一種解決方案是在單擊 div 時設置“當前”值。 當“當前”值與項目值匹配時,添加 class。
<div *ngFor="let item of items"
(click)="onItemSelection(item)
[class.current-selection]="item === currentValue">
{{ item }}
</div>
通過存儲新值來處理 component.ts 文件中的事件:
currentValue: string;
onItemSelection(newValue: string) {
this.currentValue = newValue;
}
然后更新 component.scss 文件中當前項目的外觀:
.current-selection {
background-color: green;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.