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
Disable the TextBox by adding the e-disabled to the input parent element and set disabled attribute to the input element.
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.
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With