0

I have a toggle button with multiple options. I have to set a default option to be true to begin with and this value has to be returned. I implement the button in this way

        <form novalidate>
          <mat-button-toggle-group name="testSelect" [(ngModel)]="testSelect">
            <mat-button-toggle *ngFor="let item of options" [nxValue]="item.headline">
              
                {{ item.headline }} 
            </mat-button-toggle>
          </mat-button-toggle-group>
          <p>
            Current Value: {{ testSelect }}
          </p>
        </form>

ts

export class CComponent{
testSelect: string;
options = [
{
  
  headline: 'apple'
},
{
 
  headline: 'mango'
},
{
  
  headline: 'orange'
}
]; 
}

Regarding the default option, I want the last option of the group to be preselected and its value must be recorded. Now in my case, the value is recorded only after a button is selcted.

user10183910
  • 21
  • 1
  • 7

1 Answers1

1

You can use value attribute for default selection.

<mat-button-toggle-group name="testSelect" [(ngModel)]="testSelect" value="textSelect">
    <mat-button-toggle *ngFor="let item of options" [value]="item.headline">
        {{ item.headline }}
    </mat-button-toggle>
</mat-button-toggle-group>

And in your component assign the value to textSelect which you want as default selected.

testSelect: string="orange";

Reference from this answer Link.

Vimal Patel
  • 2,785
  • 2
  • 24
  • 38
  • ,I have one question in regards to the angular material table can you please help here. https://stackoverflow.com/questions/65347251/how-to-show-split-header-in-the-material-table-having-nested-group-of-data-in-an – app Dec 18 '20 at 13:21
  • I way to default select multiple values – Hamza Haddad Jul 13 '22 at 13:04
  • @HamzaHaddad Can you explain your problem statement in detail? – Vimal Patel Jul 13 '22 at 15:07