question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RC6 Forms: disabled attribute cannot be set dynamically anymore

See original GitHub issue

I’m submitting a bug/feature request (check one with “x”)

[x] bug report => search github for a similar issue or PR before submitting
[x] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior Have a look at that, (demo: http://plnkr.co/edit/P25dYPC5ChRxpyxpL0Lj?p=preview):

@Component({
  selector: 'my-app',
  providers: [],
  template: `
    <div [formGroup]="form">
      <input formControlName="first" [disabled]="isDisabled">
    </div>
  `,
  directives: []
})
export class App {
  isDisabled = true
  form = new FormGroup({
    'first': new FormControl('Hello')
  })
}

There is a warning message asking to refactor the code, but more importantly, the input is unfortunately not disabled. Refactoring the code to what is suggested is not helping either, i.e. doing this will not work, (demo: http://plnkr.co/edit/Gf7FGR42UXkBh6e75cm2?p=preview):

@Component({
  selector: 'my-app',
  providers: [],
  template: `
    <div [formGroup]="form">
      <input formControlName="first">
    </div>
  `,
  directives: []
})
export class App {
  isDisabled = false
  form = new FormGroup({
    'first': new FormControl({value: 'hello', disabled: this.isDisabled})
  })

  constructor() {
    setTimeout(() {
      this.isDisabled = true  
    }, 10)
  }
}

Expected/desired behavior After the callback of the setTimeout is executed, the input should be disabled, but it is not.

Reproduction of the problem Yes, please look here: http://plnkr.co/edit/P25dYPC5ChRxpyxpL0Lj?p=preview

What is the motivation / use case for changing the behavior? To have the same behaviour, or similar one than in RC5. At least, we shall have a possibility, even if it’s a breaking change, to set dynamically the disabled attribute. As of now, it does not seem possible anymore.

Please tell us about your environment:

  • Angular version: 2.0.0-rc.6
  • Browser: [ Chrome 52 ]
  • Language: [ TypeScript ]

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:48
  • Comments:74 (1 by maintainers)

github_iconTop GitHub Comments

103reactions
kekehcommented, Mar 28, 2017

This worked:

<input [attr.disabled]="disabled?'':null"/>
private disabled: boolean = false;

// disable input box
disableInput(): void {
   this.disabled = true;
}

// enable input box
enableInput(): void {
   this.disabled = false;
}
68reactions
karacommented, Sep 2, 2016

@Krisa If you want to set the disabled state programmatically, you’ll need to call disable(). Changing this.isDisabled will never have an effect because it is passed by value.

export class App {
  isDisabled = false
  form = new FormGroup({
    'first': new FormControl({value: 'hello', disabled: false})
  })

  constructor() {
    setTimeout(() => {
      this.form.get('first').disable();
    }, 10)
  }
}

See example here: http://plnkr.co/edit/UXNiM8Pz73Go27nDRD7M?p=preview

Re the first example, we deliberately haven’t hooked up the template-driven entry point to reactive forms. That’s why we throw the warning. If we did hook it up, users would immediately run into ‘changed after checked’ errors because the validation of reactive forms occurs synchronously. We generally don’t suggest mixing reactive and template-driven patterns for this reason.

We considered throwing an error instead, but we were concerned that if someone had a custom form component that used disabled as an input for something unrelated, their code would break. Perhaps we can make the warning message a bit clearer. Let me know if you think the language could be changed. In any case, closing this as it works as designed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

RC6 Forms: disabled attribute cannot be set dynamically ...
I'm submitting a bug/feature request (check one with "x") [x] bug report => search github for a similar issue or PR before submitting...
Read more >
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 >
Angular 6: Disabling Input On Reactive Form Without ...
Dynamic debug is designed to allow you to dynamically enable/disable kernel code RC6 Forms: disabled attribute cannot be set dynamically anymore #11271.
Read more >
HTML attribute: disabled - HTML: HyperText Markup Language
The Boolean disabled attribute, when present, makes the element not mutable, focusable, or even submitted with the form.
Read more >
Gravity Forms Change Log
Fixed an issue where dynamically populated choices for choice-based fields cannot be used in conditional logic. Fixed an issue with the gf_list_inline ready ......
Read more >

github_iconTop Related Medium Post

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