默认情况下,如何在Angular中选择mat-button-toggle



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

<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 file

填充默认值

示例:

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;}

不要忘记在模块中导入 MatButtonToggleModule

使用formControlName将表单链接到按钮切换

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>

与Angular 12

一起工作正常

最新更新