Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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> 
like image 414
Hossein Bajan Avatar asked Dec 04 '17 16:12

Hossein Bajan


People also ask

How do I change the default value of a toggle button?

You can use value attribute for default selection. And in your component assign the value to textSelect which you want as default selected. testSelect: string="orange"; Reference from this answer Link.

How do you use the toggle group mat button?

By default, mat-button-toggle-group acts like a radio-button group- only one item can be selected. In this mode, the value of the mat-button-toggle-group will reflect the value of the selected button and ngModel is supported. Adding the multiple attribute allows multiple items to be selected (checkbox behavior).

How do I use the toggle button in angular 8?

To create a toggle button or on/off button with angular material design and animations, the Angular <mat-button-toggle> directive is used. These buttons can be configured to behave like either radio buttons or checkboxes so that a single selection or multiple selections can be done on the buttons.

What is the use of toggle in angular?

The <mat-button-toggle>, an Angular Directive, is used to create a toggle or on/off button with material styling and animations. mat-button-toggle buttons can be configured to behave as radio buttons or checkboxes.


2 Answers

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> 

like image 197
Hossein Bajan Avatar answered Sep 19 '22 07:09

Hossein Bajan


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> 
like image 43
Uliana Pavelko Avatar answered Sep 20 '22 07:09

Uliana Pavelko