[attr.disabled]=true not working in Reactive Form Angular 15
Asked Answered
E

2

11

Currently I'm using Angular 15 and in Angular 14 for the disabled input field in reactive form I used [attr.disabled]="disableField ? true : null".

This attribute after I update the Angular version 14 to 15 it's not working

Elisabetta answered 18/4, 2023 at 5:52 Comment(2)
I tried [disabled]="condition" attribute but its not working I found another solution is formControlName.disable() but I want to put condition in html codeElisabetta
use a directive to "disabled", see e.g. SOOvermaster
W
12

It's a change in Angular 15 that changes the disabled state.

This behavior change was caused by a fix to make setDisabledState always called. Previously, using [attr.disabled] caused your view to be out of sync with your model.

If you are using Reactive Forms? Try setting disabled on your model, not your template. Try new FormControl({ value: 'foo', disabled: true }). Or call myControl.disable() in ngOnInit. If you want to opt-out of the fix. Make sure you're on 15.1.0 or later and import FormsModule.withConfig({ callSetDisabledState: 'whenDisabledForLegacyCode' }) (or ReactiveFormsModule, if that's what you're using).

Wildon answered 18/4, 2023 at 6:16 Comment(1)
I have added 2 control with same formControlName as per the requirement and based on condition only one is visible at a time so I need to disabled condition in templateElisabetta
U
1

Please update your ReactiveFormsModule import statement in your module file with below statement:

ReactiveFormsModule.withConfig({callSetDisabledState: 'whenDisabledForLegacyCode'})
Uncharted answered 29/7, 2024 at 11:13 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.