简体   繁体   English

如何在角度应用程序中为选定的垫子按钮设置样式

[英]how to style a selected mat-button in an angular application

I have an Angular application where the user has multiple choices with buttons and when a button is pressed another component will display.我有一个 Angular 应用程序,用户可以在其中选择多个按钮,按下按钮时将显示另一个组件。 The component depends on the user's choice.组件取决于用户的选择。 One thing I'm trying to implement is the styling of the buttons to make it clear which choice has been selected.我试图实现的一件事是按钮的样式,以明确选择了哪个选项。

<div  fxLayout="row" fxLayoutAlign="space-between center" fxFill>
<div *ngFor="let button of buttons">
    <button
        mat-stroked-button
        *ngIf="button.type === 'button'"
        (click)="buttonPressed()"
        ngxScrollTo
    >
        {{ button.text }}
    </button>
</div>

<div  fxLayout="row" fxLayoutAlign="space-between center">
    <div *ngIf="item.hasSomeProperty | isTypeButton">
        <button mat-mini-fab (click)="buttonPressed()">
            <mat-icon>close</mat-icon>
        </button>
    </div>
</div>

I have also attached a picture of what im trying to achieve here:我还附上了我在这里尝试实现的目标的图片: 在此处输入图片说明

Any help would be much appreciated.任何帮助将非常感激。

Simply use [ngClass] or [ngStyle] :只需使用[ngClass][ngStyle]

<div *ngFor="let button of buttons">
    <button
        mat-stroked-button
        *ngIf="button.type === 'button'"
        [ngClass]="{'disabledButton': !button.selected}"
        (click)="buttonPressed(button)"
        ngxScrollTo
    >
        {{ button.text }}
    </button>
</div>

Assuming that your button model contains the "selected" property (or you have some other model storing information which button was actually clicked):假设您的按钮模型包含“selected”属性(或者您有一些其他模型存储实际单击哪个按钮的信息):

buttonPressed(button: Button) {
    // Mark only button as selected
    this.buttons.forEach(b => b.selected = b === button);
}

And of course add some class in the css:当然,在 css 中添加一些类:

.disabledButton {
     opacity: 0.75;
}

Note - writing this from top of my head (since no stackblitz was provided) so some adjustments might be needed.注意 - 从我的头顶写这个(因为没有提供 stackblitz)所以可能需要一些调整。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM