I already tried to follow the example of other answers from here and I did not succeed!
I created a reactive form (ie, dynamic) and I want to disable some fields at any given time. My form code:
this.form = this._fb.group({
name: ['', Validators.required],
options: this._fb.array([])
});
const control = <FormArray>this.form.controls['options'];
control.push(this._fb.group({
value: ['']
}));
my html:
<div class='row' formArrayName="options">
<div *ngFor="let opt of form.controls.options.controls; let i=index">
<div [formGroupName]="i">
<select formArrayName="value">
<option></option>
<option>{{ opt.controls.value }}</option>
</select>
</div>
</div>
</div>
I reduced the code to facilitate. I want to disable the field of type select. I tried to do the following:
form = new FormGroup({
first: new FormControl({value: '', disabled: true}, Validators.required),
});
not working! Does anyone have a suggestion?
If you want to disable
first
(formcontrol) then you can use below statement.this.form.first.disable();
I solved it by wrapping my input object with its label in a field set: The fieldset should have the disabled property binded to the boolean
A more general approach would be.
It is bad practice to use disable in a DOM with reactive forms. You can set this option in your
FormControl
, when you init the fromProperty
value
is not necessaryOr you can get your form control with
get('control_name')
and setdisable
This worked for me:
this.form.get('first').disable({onlySelf: true});
or