Based on this topic's accepted answer, I managed to get the selected state of the clicked option. I want to disable an input based on a selection of a certain option. With the below example, I am able to enable/disable the input when any option is checked/unchecked. For example, I want to disable the input field only if the second option (Mushroom) is unchecked. If the second option is checked, the input field could be enabled. How could I do it?
Asked
Active
Viewed 126 times
0
-
are you using angular forms to preserve data? – Storytellerr Apr 19 '23 at 08:02
-
All my `mat-form-field` are in ` – Ionut Apr 19 '23 at 08:05
-
https://stackoverflow.com/questions/57496789/how-to-know-if-a-mat-option-is-selected-or-not-from-a-click-event – pero_hero Apr 19 '23 at 08:38
2 Answers
1
You can achive this by attaching your formcontrol value to mat-input's [disabled] directive.
1)just read toppings(form control) value
isValueSelected(): boolean {
var valueArray: any = this.toppings.value || [];
console.log(valueArray, valueArray.includes('Mushroom'));
return !valueArray.includes('Mushroom');
}
2)attach it to [disabled] directive
<input id="numeDB_SQLite" type="text" name="numeDB_SQLite" [disabled]="isValueSelected()"/>
i have updated your stackblitz here. please upvote/select if helps.

Storytellerr
- 642
- 3
- 18
0
If want disabled if some value is selected
<input id="numeDB_SQLite" type="text" matInput
[disabled]="toppings?.value && toppings.value.length" ../>
If want disabled when one element is selected, e.g.
<input id="numeDB_SQLite" type="text" matInput
[disabled]="toppings?.value && toppings.value.includes('Mushroom')" ../>

Eliseo
- 50,109
- 4
- 29
- 67