Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular *ngFor "id" tag generation incorrect with `mat-button-toggle`

I'm trying to generate a list of buttons from an array:

<div class="card-container">
    <mat-button-toggle *ngFor="let button of buttonsFromApi" id={{button.id}} class="problemButton" [disabled]="sso.invalid" >{{button.id}}</mat-button-toggle>
</div>

buttonsFromApi contains 5 objects like:

{
    displayName: "Button name", 
    id: "100",
}

So basically I want the button to display the displayName (this works) and to have the id that is in the object. The latter of this does not happen, but instead, the button(s) have id(s) like 100-button and 200-button instead of 100 and 200. Why is this and how can I make it so the ids are 100 and 200 instead of 100-button and 200-button?

like image 945
lte__ Avatar asked Apr 01 '26 01:04

lte__


2 Answers

if you want to override (or better extend) the default-button-id from angular material, you have to pass in the id. (https://material.angular.io/components/button-toggle/api#MatButtonToggle)

<div>
  <mat-button-toggle id="test-123" value="right" aria-label="Text align right"></mat-button-toggle>
</div>

the you will get your id inside the component:

<mat-button-toggle ... id="test-123">
  <button class="mat-button-toggle-button" type="button" id="test-123-button" tabindex="0" aria-pressed="false" aria-label="Text align right">
<div class="mat-button-toggle-label-content"></div>
</button>
...
</mat-button-toggle>
like image 161
deelde Avatar answered Apr 02 '26 15:04

deelde


Id should be assigned in quotes

<div *ngFor="let button of buttonsFromApi" id="{{button.id}}">
  <mat-button-toggle>{{button.displayName}}</mat-button-toggle>
</div>
like image 39
ranjeet8082 Avatar answered Apr 02 '26 14:04

ranjeet8082



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!