I have an Angular Material dropdown menu, and I need to disable specific elements from that dropdown, based on which value is selected from another dropdown menu.
Here is the HTML for the two dropdowns:
<mat-form-field>
<mat-label>Choose an option</mat-label>
<mat-select id="lineOfBusiness" formControlName="lineOfBusiness">
<mat-option value="commercial">Commercial</mat-option>
<mat-option value="residential" >Residential</mat-option>
<mat-option value="industrial">Industrial</mat-option>
</mat-select>
</mat-form-field>
<mat-form-field>
<mat-label>Choose an option</mat-label>
<mat-select id="psv" formControlName="psv">
<mat-option value="inclinometer">Inclinometer</mat-option>
<mat-option value="heil" >Heil</mat-option>
<mat-option value="radar">Radar</mat-option>
<mat-option value="hydraulics">Hydraulics</mat-option>
<mat-option value="coretex">Coretex</mat-option>
</mat-select>
</mat-form-field>
For example, the logic would look something like this: If 'lineOfBusiness' = 'commercial', then in the 'psv' dropdown, 'radar' should be disabled.
In my form.component.ts file, I have this function, which is executing without error, but is not disabling the needed options.
disableOption(optionValue: string): void {
console.log("Disabling " + optionValue);
const psvControl = this.reactiveForm.get('psv');
if (psvControl) {
const option = psvControl.get(optionValue);
if (option) {
option.disable();
}
}
}
What am I doing wrong?
Key concept:
Get the MatSelect DOM element for
psvcontrol via the template reference variable.Subscribe to the
lineOfBusinesscontrol'svalueChangesto fire thedisableOptionmethod when thelineOfBusinessvalue is changed.Set the mat-option disabled when the value "commercial" is selected for the
lineOfBusinesscontrol and vice versa.You need to declare the template reference variable
#psvSelectas below:Demo @ StackBlitz