Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 4 select disabled is not working

I want to disable select in Angular 4. I wrote code like below but select is not disabled.

In the component:

import { FormBuilder, Validators, FormGroup } from '@angular/forms';    

this.eventForm = this.formBuilder.group({
    // ... some codes ...
    'state': [true, Validators.required],
    'stepStrengh': [10, Validators.nullValidator]
});

In HTML:

<input type="radio" formControlName="state" [value]="true"/> Enable Step
<input type="radio" formControlName="state" [value]="false"/> Disable Step

<select formControlName="stepStrengh" [disabled]="!eventForm.get('timeslots').value">
  <option [ngValue]="10">10 steps</option>
  <option [ngValue]="15">15 steps</option>
</select>

When I add [disabled]="true" into option tag, then that option tag is disabled, but I want to disable the select tag itself.

And I tried like this -

<select formControlName="stepStrengh" disabled="{{state ? '': 'disabled'}}">

But this is also not working, when I change the state variable from true->false or false->true

like image 304
Shine Han Avatar asked Mar 03 '18 18:03

Shine Han


People also ask

How to make input disabled in angular?

Disable the TextBox by adding the e-disabled to the input parent element and set disabled attribute to the input element.

How to disable a attribute in angular?

AngularJS ng-disabled Directive The ng-disabled directive sets the disabled attribute of a form field (input, select, or textarea). The form field will be disabled if the expression inside the ng-disabled attribute returns true.

How do you enable disable a dropdown based on value in another dropdown in angular?

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 disabled the DropDownField via [disabled]="..." , then you will get much yellow text in your console. This answer worked for me.

How do I turn off reactive dropdown?

If you set disabled to true when you set up this control in your component class, the disabled attribute will actually be set in the DOM for you. We recommend using this approach to avoid 'changed after checked' errors. Example: form = new FormGroup({ first: new FormControl({value: 'Nancy', disabled: true}, Validators.


2 Answers

After reading some docs and other stuff I found out that this approach is working for me

<select [attr.disabled]="state ? '' : null" formControlName="stepStrengh" >

hope it helps.

Here is a link to stackblitz

like image 128
Kraken Avatar answered Sep 21 '22 05:09

Kraken


Your kind of mixing the template driven way with the reactive form way. It's best to choose one approach. In this case in your typescript do: control.disable()

Specifically for your purpose it would be:

this.eventForm.get('stepStrengh').disable();

Note you would set this AFTER your initial form set-up.

like image 23
Andrew Junior Howard Avatar answered Sep 19 '22 05:09

Andrew Junior Howard