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.

2
  • What is the issue you are facing? Commented Dec 10, 2020 at 11:44
  • 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. @VimalPatel Commented Dec 10, 2020 at 11:54

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

,I have one question in regards to the angular material table can you please help here. stackoverflow.com/questions/65347251/…
I way to default select multiple values
@HamzaHaddad Can you explain your problem statement in detail?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.