1

How can I enable/disable a dropdown based on the selection of another dropdown menu?

I tried this but it doesn't seem to work:

<select class="custom-select" id="abc">
    <option value="" hidden>Select</option>
    <option *ngFor="let data of datay" 
            [ngValue]="data.code" 
            [attr.disabled]="true"> 
        {{data.name}}
    </option>
</select>

Any sample code is appreciated.

3
  • Ok, just to get it clear, are you trying to disable another dropdown based on the value you have selected on your current dropdown? Commented May 30, 2019 at 13:37
  • @wentjun , yeah Commented May 30, 2019 at 13:38
  • 1
    @Developer, you can use [attr.disabled] like [attrib.disabled]="condition?true:null" where condition is a contidion that you want. if you use [attr.disabled]="false" not work, you must use "null" to not render the attribute Commented May 30, 2019 at 16:43

4 Answers 4

2

Here is example with reactive forms as answer with template-driven forms is already posted:

<form [formGroup]="form">
  <select name="field1" formControlName="field1">
    <option *ngFor="let value of [1, 2, 3]" [value]="value">{{value}}</option>
  </select>
</form>

<form [formGroup]="form">
  <select name="field1" formControlName="field2">
    <option *ngFor="let value of [1, 2, 3]" [value]="value">{{value}}</option>
  </select>
</form>

TS:

  form;

  ngOnInit(): void {
    this.form = new FormGroup({
      field1: new FormControl(),
      field2: new FormControl()
    });

    this.form.controls.field1.valueChanges.subscribe(value => {
      value === '2' ? this.form.controls.field2.disable() : this.form.controls.field2.enable();
    });
  }
Sign up to request clarification or add additional context in comments.

1 Comment

I'm using form builder and tried this : this.form.fb.field1.value.subscribe(value=>{ value === 'ABC' ? this.form.controls.field2.disable() : this.form.controls.field2.enable(); }); is this the right way to do ?
1

We can do this by using 2-way data binding. First, we bind the first <select> element to data1. This will contain the value data.code of the selected option from the first element.

Let's assume that we will disable the second <select> element if the user selects the option with the data.code value of '1'.

Then, on our second <select> element, we bind the disabled attribute to the condition, such that the disabled arribute will be true when data1 === '1'.

<select class="custom-select" [(ngModel)]="data1" id="dropdown1">
  <option value="" hidden>Select</option>
  <option *ngFor="let data of datay" [ngValue]="data.code"> 
    {{data.name}} 
  </option>
</select>

<select class="custom-select" [(ngModel)]="data2" [disabled]="data1==='1'">
  <option value="" hidden>Select</option>
  <option *ngFor="let data of datay2" [ngValue]="data.code"> 
    {{data.name}} 
  </option>
</select>

3 Comments

<select class="custom-select" [(ngModel)]="data1" id="dropdown1"> <option value="" hidden>Select</option> <option *ngFor="let data of datay" [ngValue]="data.code"> {{data.name}} </option> </select> <select class="custom-select" [(ngModel)]="data2" [disabled]="data1==='1'"> <option value="" hidden>Select</option> <option value="test"></option> <option value="test2"></option> </select>
it is string value.
@Developer can you look at this demo(stackblitz.com/edit/angular-hkxeei?file=src/app/…) that I have made instead? I am pretty sure it works :) the second select will be disabled if you select 1 on your first select
1

Give this a try:

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  selectedFruit = null;
  fruits = [
    { name: 'Apple', code: 'Apple' },
    { name: 'Mango', code: 'Mango' },
    { name: 'Orange', code: 'Orange' },
    { name: 'Grapes', code: 'Grapes' },
    { name: 'Peach', code: 'Peach' },
  ];

  models = [
    { name: 'iPhone XS Max', code: 'iPhone XS Max' },
    { name: 'iPhone XS', code: 'iPhone XS' },
    { name: 'iPhone X', code: 'iPhone X' },
    { name: 'iPhone 8 Plus', code: 'iPhone 8 Plus' },
    { name: 'iPhone 8', code: 'iPhone 8' },
  ]
}

And in your template:

<h2>Fruits</h2>
<select class="custom-select" id="abc" [(ngModel)]="selectedFruit">
  <option value="null" hidden>Select</option>
  <option *ngFor="let fruit of fruits" [ngValue]="fruit.code"> {{fruit.name}}</option>
</select>

<h2>Phone Models</h2>
<select class="custom-select" id="abc" [disabled]="selectedFruit !== 'Apple'">
  <option value="" hidden>Select</option>
  <option *ngFor="let model of models" [ngValue]="model.code"> {{model.name}}</option>
</select>

Here's a Working Sample StackBlitz for your ref.

Comments

1

It depends on if the dropdown is connected to a FormGroup.

If yes, simply disable the FormControl with formGroup.get('yourPath').disable({onlySelf: true});

If not, you can also just use the [disabled]="isFieldDisabled" property on the <select> property.

If you use a FormGroup with your DropDownField but choose to disable the DropDownField via [disabled]="...", then you will get much yellow text in your console.

1 Comment

This answer worked for me. IonicMan is also correct with his last sentence - in Chrome if the Default levels dropdown in the Console area is set to show warnings, you will see a lot of yellow text (i.e. warnings) about the recommended approach for setting the disabled attribute.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.