I am trying to use the disabled
attribute from a formControl
. When I put it in the template, it works:
<md-input formControlName="id" placeholder="ID" [disabled]="true"></md-input>
But the browser alerts me:
It looks like you're using the disabled attribute with a reactive form directive. 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.required), last: new FormControl('Drew', Validators.required) });
So I put it in the FormControl
, and deleted from the template:
constructor(private itemsService: ItemsService) {
this._items = [];
this.myForm = new FormGroup({
id: new FormControl({value: '', disabled: true}, Validators.required),
title: new FormControl(),
description: new FormControl()
});
this.id = this.myForm.controls['id'];
this.title = this.myForm.controls['title'];
this.description = this.myForm.controls['description'];
this.id.patchValue(this._items.length);
}
But it does not work (it is not disabling the input
). What is the problem?
In my case with Angular 8. I wanted to toggle enable/disable of the input depending on the condition.
[attr.disabled]
didn't worked for me so here is my solution.I removed
[attr.disabled
from HTML and in the component function.Initialization (component) using:
Then instead of using (template):
Just remove the attribute disabled:
And when you have items to show, launch (in component):
this.selector.enable();
You can enable/disable a form control by using the following methods:
control.disable() or control.enable()
If that did not work for you can use a directive
Then you could use it like this
This technique is described here:
https://netbasal.com/disabling-form-controls-when-working-with-reactive-forms-in-angular-549dd7b42110
Hope it helps
Use [attr.disabled] instead [disabled], in my case it works ok
add disable="true" to html field Example :disable
add name attribute to your md-input. if it doesn't solve the problem, please post your template