I need to modify the functionality of the standard Angular Material toggle button component, so that clicking an active button returns the component to a state where no buttons are active. This has two steps:
- Updating the value of the toggle group
- Changing the 'checked' parameter of the clicked button to false
I've tried several approaches, e.g.
Template:
<mat-button-toggle-group #group="matButtonToggleGroup">
<mat-button-toggle #no_btn value="no" (click)="update_toggle(group,no_btn)">No</mat-button-toggle>
<mat-button-toggle #yes_btn value="yes" (click)="update_toggle(group,yes_btn)">Yes</mat-button-toggle>
</mat-button-toggle-group>
JS:
update_toggle(group,button){
if(group.value==""){
group.value = button.value;
}
else
{
group.value = "";
}
button.checked=!button.checked;
}
But this doesn't update the 'checked' value of the buttons, I guess because the group value set by update_toggle() is in conflict with the user action of clicking the button.
The only approach which has worked is:
<mat-button-toggle-group #group="matButtonToggleGroup">
<mat-button-toggle #no_btn value="no" (click)="update_toggle(group,no_btn)" (click)="group.value=='no' ? checked=false : checked=false">No</mat-button-toggle>
<mat-button-toggle #yes_btn value="yes" (click)="update_toggle(group,yes_btn)" (click)="group.value=='yes' ? checked=false : checked=false">Yes</mat-button-toggle>
</mat-button-toggle-group>
But two click events on a single button feels very hacky, especially as the ternary in the second click is opposite to what it instinctively should be.
Any suggestions for a better approach?
I've tried:
@ViewChildren('no_btn') no_btn: ElementRef;
and then:
this.no_btn['_results'][k]['_inputElement']['nativeElement']['checked']=false;
But the result doesn't seem to be any different from passing the button reference in the function; clicking the button a second time doesn't uncheck it. Disabling does work, so I think my code is correct:
this.no_btn['_results'][k]['_inputElement']['nativeElement']['disabled']=true;
@ViewChild()'s to get the instance of the checkbox and change it that way?