问题是,当您单击其他按钮时,您没有重置isclicked值。
最简单的解决办法就是这样。
app.component.html软件
<button mat-button *ngFor="let button of filterButtons" [ngClass]="{'active': button.isClicked}" (click)="setActive(button)">
{{ button.text }}
</button>
应用组件
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
filterButtons = [
{ text: 'Posted', isClicked: false },
{ text: 'FFM', isClicked: false },
{ text: '9999', isClicked: false },
]
setActive(button: any): void {
for(let but of this.filterButtons) {
but.isClicked = false;
}
button.isClicked = true;
}
}
您可以找到其他解决方案,如
this post