Changing a [formControl] reference from a disabled control to an enabled control leaves the HTML control disabled
See original GitHub issueBug Report
Affected Package
The issue is caused by package @angular/forms
Is this a regression?
Unsure - happens in at least the last few versions
Description
Say I have an element <input [formControl]="selectedControl" />
, when selectedControl
is changed in the component TS from a disabled control to an enabled control, the rendered HTML input
element remains disabled.
Minimal Reproduction
https://stackblitz.com/edit/angular-form-reference-disabled-bug?file=src/app/app.component.ts
- Click “Select Control 2” - the HTML input will correctly be updated with the value 2, and the disabled state.
- Click “Select Control 1” - the HTML input will correctly be updated with the value 1, but it will incorrectly remain disabled.
Your Environment
Angular Version:
The stackblitz is on 11.0.8. This was repro-ed locally on an app using:
_ _ ____ _ ___
/ \ _ __ __ _ _ _| | __ _ _ __ / ___| | |_ _|
/ △ \ | '_ \ / _` | | | | |/ _` | '__| | | | | | |
/ ___ \| | | | (_| | |_| | | (_| | | | |___| |___ | |
/_/ \_\_| |_|\__, |\__,_|_|\__,_|_| \____|_____|___|
|___/
Angular CLI: 11.0.7
Node: 14.15.1
OS: win32 x64
Angular: 11.2.8
... animations, cdk, common, compiler, compiler-cli, core, forms
... language-service, localize, platform-browser
... platform-browser-dynamic, router
Ivy Workspace: <error>
Package Version
---------------------------------------------------------
@angular-devkit/architect 0.1100.7
@angular-devkit/build-angular 0.1102.8
@angular-devkit/core 11.2.7
@angular-devkit/schematics 11.2.7
@angular/cli 11.0.7
@angular/flex-layout 11.0.0-beta.33
@schematics/angular 11.2.7
@schematics/update 0.1100.7
rxjs 6.6.3
typescript 4.0.7
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
How to disable form control but keep value - Stack Overflow
We recommend using this approach to avoid 'changed after checked' errors. Example: form = new FormGroup({ first: new FormControl({value: 'Nancy', disabled: ...
Read more >Exploring the difference between disabling a form control ...
Exploring the difference between disabling a form control through reactive forms API and HTML attributes · Using the FormControl's disable() instance method.
Read more >Disabling Form Controls When Working With Reactive Forms ...
You can enable/disable a form control by using the following ways: Instantiate a new FormControl with the disabled property set to true.
Read more >Edit form and Display form controls in Power Apps
If you add an Edit form control, the user can edit those fields, create a record, and save those changes to a data...
Read more >Forms · Bootstrap v4.6
Read-only inputs appear lighter (just like disabled inputs), but retain the standard cursor. Copy. <input class="form-control ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Right, #41881 looks like my issue, but I don’t accept that #41881 is actually a duplicate #22556 . At least, nothing in either issue suggest this is the case. I found #22556 when searching before raising my issue, read the thread, and decided #22556 was not a duplicate.
I’m fairly certain the solution to my issue lies in
FormControlDirective#ngOnChanges
. Afaict #22556 is about the handling ofFormControl#disable
calls. I could well be wrong but these seem unlikely to be related?Notably, also, in #22556 a lot of people are talking about their controls not disabling when they should. That isn’t the case here. With my issue, controls disable when they should, only the behaviour when enabling is wrong (or, more accurately, when changing the reference from a disabled control to an enabled control).
I believe the root cause is the same as in the referenced issue. You can see https://github.com/angular/angular/issues/41881 or others where the commands are similar to what you are doing.