patterntypescriptangularCritical
Reactive forms - disabled attribute
Viewed 0 times
attributeformsreactivedisabled
Problem
I am trying to use the
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.
So I put it in the
But it does not work (it is not disabling the
disabled attribute from a formControl. When I put it in the template, it works: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?Solution
I have been using
Change
to
If you are on newer material change
[attr.disabled] because I still like this template driven than programmatic enable()/disable() as it is superior IMO.Change
to
If you are on newer material change
md-input to mat-input.Code Snippets
<md-input formControlName="id" placeholder="ID" [disabled]="true"></md-input><md-input formControlName="id" placeholder="ID" [attr.disabled]="true"></md-input>Context
Stack Overflow Q#40494968, score: 205
Revisions (0)
No revisions yet.