[attr.disabled] on reactive form not working as expected

See original GitHub issue

Bug Report

Affected Package

The issue is caused by package @angular/material or @angular/forms

Is this a regression?

I have no idea, when I’m checking on internet, it seems that it worked some time ago, there’s a lot of place where you’re asked to use [attr.disabled].

Description

I’m using a Reactive Form to control a mat-slide-toggle.

Let’s say I have 2 mat-slide-toggle, the first one disable the second one and the second one disable the first one. I’d like to achieve this by using [attr.disabled].

If i’m using [disabled] it work as expected but I’m getting a warning in the console, saying that it’s modifying the DOM.

Minimal Reproduction

In this example the first one should be disable as I’m using [attr.disabled]="true" and the second one enabled as I’m using [attr.disabled]="false" (even if it’s enabled by default).

If I put directly in the constructor ... { value: false, disabled: true} ... for the first one, then it’s being properly disabled.

I’d like to control this behavior using [attr.disabled].

https://stackblitz.com/edit/angular-ew3gr6

Your Environment

Also happen on Angular 12 since it’s what’s being used by stackblitz.


     _                      _                 ____ _     ___ 
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | | 
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | | 
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/

Angular CLI: 11.2.11
Node: 14.16.1
OS: win32 x64

Angular: 11.2.12
... animations, common, compiler, compiler-cli, core, forms
... language-service, localize, platform-browser
... platform-browser-dynamic, router
Ivy Workspace: Yes

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1102.11
@angular-devkit/build-angular   0.1102.11
@angular-devkit/core            11.2.11
@angular-devkit/schematics      11.2.11
@angular/cdk                    11.2.11
@angular/cli                    11.2.11
@angular/material               11.2.11
@schematics/angular             11.2.11
@schematics/update              0.1102.11
rxjs                            6.6.7
typescript                      4.1.5

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Airbladercommented, May 21, 2021

You need to enable/disable the component through the form control associated with it (FormControl#enable, FormControl#disable). You can subscribe to the value changes of each control to trigger disabling the other one. That’s what the “reactive” in “reactive forms” stands for, as opposed to the template-driven approach.

0reactions
angular-automatic-lock-bot[bot]commented, Jun 21, 2021

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

angular - Reactive forms - disabled attribute - Stack Overflow
I have been using [attr.disabled] because I still like this template driven than programmatic enable()/disable() as it is superior IMO.
Read more >
How to handle reactive way for disabled property for input e
We have to send separate disabled property by using attr.disabled attribute. By doing this it is working but showing unwanted console logs which...
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. Having parts of a form disabled is ...
Read more >
Disabling Form Controls When Working With Reactive Forms ...
It looks like you're using the disabled attribute with a reactive form directive. If you set disabled to true when you set up...
Read more >
Can't Bind to formGroup Not Known Property Error in Angular
Angular is trying to tell us that it doesn't know about the formGroup directive on the <form> element in your component. This usually...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found