RC6 Forms: disabled attribute cannot be set dynamically anymore
See original GitHub issueI’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:
- Created 7 years ago
- Reactions:48
- Comments:74 (1 by maintainers)
This worked:
@Krisa If you want to set the disabled state programmatically, you’ll need to call
disable()
. Changingthis.isDisabled
will never have an effect because it is passed by value.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.