I’m not sure whether I’m being a bit stupid or not but what I want to do is display two toggles and when either one is clicked, the other toggle is unclicked i.e they are the reverse of each other and only one can be set to ON. I first tried using the (ionChange) event on the toggles but when one of the toggles sets the value of the other toggle, that toggle’s event fires and you end up in an endless loop (is there a way around that?). My next option was to forget (ionChange) all-together and instead simply use the [checked] input. So what I have is:
The strange things is that this only works for the SECOND toggle on the page. If I switch the second toggle, it correcly switches the first toggle off and on. But whenever I click the first toggle I get the error message:
ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: ‘true’. Current value: ‘false’
Does anyone know what’s going on here? And is there a better way??
I don’t have a choice here - the requirement from the customer is for two toggles with only one toggle being allowed to be selected so that if either is selected the other toggle is automatically unselected (and vice versa). I’m sure this is easily do-able - I’m just not seeing it!
It’s actually quite a bit harder than it looks. I think you are best off going back to the first solution with (ionChange) and manually guarding against the problem of it being called too often. See this thread.
Just have the (ionChange) event of the radio button group fire ChangeDetectorRef.detectChanges() and your ngIf’s will update. I’ve used that structure with multiple variations of radio buttons, ion-selects, ion-checkboxes, forms that contain various types of inputs.
It shouldn’t, that’s why we’ve coded it in two methods in the first place. When the first change event automatically triggers the second change event, the condition is already met and the second one will simply stop. Just check it out.
Yeah you must be using a single method for it so what was happening is that when the first change event triggered the other, a kind of recursion was starting up. If you play the flow of this in your mind, you’ll find that this solution works because we’ve divided the functions into two and because we are using a not operator rather than directly setting any true false values.
I had the same issue but I solved in a different way than those gentlemen’s solutions. they suggested to use two functions as handlers to ionChange event , only in my case I don’t specifically know how many items i’ll have in the run time so I had to use *ngFor which means i can’t use their solutions so I came up with the following:
1- make ion-toggle disabled <ion-toggle disabled [checked]="values[i]"></ion-toggle>
2- on ion-item add a click event and do your logic there <ion-item *ngFor="let floor of floors; let i = index" (click)="onSelectFloor(i)">
3- override the disable theme and make it look normal $toggle-ios-disabled-opacity: 1
or if you want to override the theme only in a specific page: