64

How can I set default selected last button in toggle group.
This is my code.

<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>
Hossein Bajan
  • 2,644
  • 5
  • 19
  • 36

7 Answers7

105

I fixed it. Simply add the value attribute to the mat-button-toggle-group tag.

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

Marcos Dimitrio
  • 6,651
  • 5
  • 38
  • 62
Hossein Bajan
  • 2,644
  • 5
  • 19
  • 36
  • 1
    I vote up but you may also see on [github](https://github.com/angular/angular/issues/20793) –  Dec 29 '17 at 12:05
55

Hope this will help someone.

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
  • 2,824
  • 28
  • 32
11

@Uliana Pavelko's answer is awesome. But what would be for multiple selected button group?

Bellow is the example. Don't forget to pass values as string in

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> 
Juri Noga
  • 4,363
  • 7
  • 38
  • 51
souravmsh
  • 638
  • 6
  • 11
3

For people using a FormBuilder, I propose to fill the default value from .ts file

example:

formControlName : ['All', Validators.required]
Joe Mayo
  • 7,501
  • 7
  • 41
  • 60
1

In case someone got stuck on this. I save the object on my value attribute and set the checked attribute for index === 0.

<mat-button-toggle [value]="object" *ngFor="let object of objectsList; let i = index" [checked]="i === 0">{{object.name }}</mat-button-toggle>
p90n33r
  • 1,671
  • 3
  • 9
  • 6
0

In case you are using formcontrolName and it still doesn't take your byDefault value, then try this, it works for me:

<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;}
Jayme
  • 1,776
  • 10
  • 28
0

Don't forget to import MatButtonToggleModule in your module.ts

Use formControlName to link the form to the button toggle

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>

Works fine with Angular 12

Jaraxxus
  • 116
  • 1
  • 5