question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Toggle Button doesn't highlight the selected value if it is a number

See original GitHub issue

Bug, feature request, or proposal:

Bug (regression?)

What is the expected behavior?

Before the rewrite of the button toggle class it seemed to try to coerce values when comparing them so you could set the value of a button like value="1" and it would see that button as the selected one when the model behind had a value of 1.

What is the current behavior?

It now does an exact comparison to the model behind and won’t select the button with value="1" if the model has that value.

What are the steps to reproduce?

https://stackblitz.com/edit/angular-wwwlek?file=app%2Fbutton-toggle-exclusive-example.html

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

Angular 6, Material 6

Is there anything else we should know?

Maybe this is the way we want it to work, it was just a change that wasn’t really documented and happened to break part of my site. Feel free to close this if it’s how you want this to work, I was mainly putting this here so it might show up in someone else’s search results since there is a work around.

In my project I have a list of parts that have some status codes associated with them when they do some service. These codes are stored in the system as numbers, but the codes that are possible don’t change so I had something like this (greatly simplified here):

           <mat-button-toggle-group [(ngModel)]="part.code">
                <mat-button-toggle *ngIf="part.allowNa" value="-1">N/A</mat-button-toggle>
                <mat-button-toggle class="good" value="0">Good</mat-button-toggle>
                <mat-button-toggle class="bad" value="1">Replaced</mat-button-toggle>
            </mat-button-toggle-group>

This no longer works as it doesn’t see 1 as being equal to "1" and there’s no way to say “this value is a number not a string” when you define it in the HTML. The work around is to do this:

naVal = -1;
goodVal = 0;
badVal = 1;
...
           <mat-button-toggle-group [(ngModel)]="part.code">
                <mat-button-toggle *ngIf="part.allowNa" [value]="naVal">N/A</mat-button-toggle>
                <mat-button-toggle class="good" [value]="goodVal">Good</mat-button-toggle>
                <mat-button-toggle class="bad" [value]="badVal">Replaced</mat-button-toggle>
            </mat-button-toggle-group>

This is probably a better way of doing it anyways since badVal means more to someone else reading this than 1 does. Just raising this in case it wasn’t intentional

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
josephperrottcommented, May 30, 2018

Closing, as you noted this working as expected. If it was not working this way in the past, then the change in behavior should be considered a bug fix.

0reactions
angular-automatic-lock-bot[bot]commented, Sep 11, 2019

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Material Button Toggle is not sync with model
When ONE and TWO were selected, ALL should be toggled and ONE and TWO should be toggled off. But then the last selected...
Read more >
Button toggle
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...
Read more >
A mouse click does not highlight toggle boxes and buttons
A mouse click does not highlight toggle boxes and buttons; no focus decorator / rectangle is shown.
Read more >
SWITCH function
If there is no match, an optional default value may be returned. If there are no matching values, and no default argument is...
Read more >
Change Switch Access settings - Android Accessibility Help
On the Switch Access settings screen, select Assign switches for scanning. ... Tip: Previous does not undo actions, such as opening an app....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found