如何在 angular 中默认选择 mat-button-toggle

How to mat-button-toggle by default selected in angular

如何在切换组中设置默认选择的最后一个按钮。
这是我的代码。

<mat-button-toggle-group #group="matButtonToggleGroup">
    <mat-button-toggle value="Heritage">
        <span>Heritage</span>
    </mat-button-toggle>
    <mat-button-toggle value="Nature">
        <span>Nature</span>
    </mat-button-toggle>
    <mat-button-toggle value="People">
        <span>People</span>
    </mat-button-toggle>
    <mat-button-toggle value="All">
        <span>All</span>
    </mat-button-toggle>
</mat-button-toggle-group>

我修好了。只需将 value 属性添加到 mat-button-toggle-group 标签即可。

<mat-button-toggle-group #group="matButtonToggleGroup" value="All">
<mat-button-toggle value="Heritage">
    <span>Heritage</span>
</mat-button-toggle>
<mat-button-toggle value="Nature">
    <span>Nature</span>
</mat-button-toggle>
<mat-button-toggle value="People">
    <span>People</span>
</mat-button-toggle>
<mat-button-toggle value="All">
    <span>All</span>
</mat-button-toggle>

希望这会对某人有所帮助。

public selectedVal: string;
constructor() { }

ngOnInit(){
  this.selectedVal ='option1';
} 

public onValChange(val: string) {
  this.selectedVal = val;
}

 <mat-button-toggle-group #group="matButtonToggleGroup" [value]="selectedVal" (change)="onValChange(group.value)" >
  <mat-button-toggle value="option1">
    Option 1
  </mat-button-toggle>
  <mat-button-toggle value="option2">
    Option 2
  </mat-button-toggle>
</mat-button-toggle-group>

@Uliana Pavelko 的回答很棒。但是多选按钮组会是什么?

下面就是例子。不要忘记将值作为字符串传递给

public selectedVal: string;
constructor() { }

ngOnInit(){
  this.selectedVal =['2','6'];
}

public onValChange(val: string) {
  this.selectedVal = val;
}

<mat-button-toggle-group #group="matButtonToggleGroup" [value]="selectedVal" (change)="onValChange(group.value)" >
<mat-button-toggle value="option1">
    Option 1
</mat-button-toggle>
<mat-button-toggle value="option2">
    Option 2
</mat-button-toggle>
</mat-button-toggle-group> 

对于使用 FormBuilder 的人,我建议填写 .ts 文件中的默认值

示例:

formControlName : ['All', Validators.required]

以防有人卡在这上面。我将对象保存在 value attribute 上并为 index === 0 设置 checked attribute

<mat-button-toggle [value]="object" *ngFor="let object of objectsList; let i = index" [checked]="i === 0">{{object.name }}</mat-button-toggle>

如果您使用的是 formcontrolName,但它仍然没有采用您的 byDefault 值,那么试试这个,它对我有用:

<mat-button-toggle-group formControlName="boolValue" aria-label="Font Style">
    <mat-button-toggle value="false" [ngClass]="Form.controls['boolValue'].value ? '':'active'">Disable</mat-button-toggle>
    <mat-button-toggle value="true" [ngClass]="Form.controls['boolValue'].value ? 'active':''">Enable</mat-button-toggle>
</mat-button-toggle-group>

style.css

.active {background-color: #e0e0e0 !important;}

不要忘记在 module.ts

中导入 MatButtonToggleModule

使用formControlName将表单link切换到按钮

test.component.ts

import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';

@Component({
    selector: 'app-test',
    templateUrl: './test.component.html',
    styleUrls: ['./test.component.scss']
})
export class TestComponent implements OnInit {
    public form: FormGroup;

    constructor(private formBuilder: FormBuilder) {
        this.form = this.formBuilder.group({
            choices: [1] // <-- Default value 1
        });
    }

    ngOnInit(): void {}
}

test.component.html

<form [formGroup]="form">
    <mat-button-toggle-group formControlName="choices">
        <mat-button-toggle [value]="1">Choice 1</mat-button-toggle>
        <mat-button-toggle [value]="2">Choice 2</mat-button-toggle>
        <mat-button-toggle [value]="3">Choice 3</mat-button-toggle>
    </mat-button-toggle-group>
</form>

适用于 Angular12