angular: Binding on input [type] is broken
I’m submitting a …
[x] bug report => search github for a similar issue or PR before submitting
Current behavior
Try to bind the type attribute of input:
// in Class
inputType="number"
// in html
<input [type]="inputType" formControlName="integerB" />
Initially, the value is presented as number when doing form.value:
{"integerB": 5}
Now, try to change the input’s value.
Its type has changed to string:
{"integerB": "5"}
while it seems the html attributes haven’t changed:
// chrome dev console
<input formcontrolname="integerB" ng-reflect-name="integerB" ng-reflect-type="number" type="number" class="ng-valid ng-dirty ng-touched">
Expected behavior If we are able to bind to [type], the input type should have stayed “number”, so does the value.
Minimal reproduction of the problem with instructions http://plnkr.co/edit/kp0xoDbZmJnD5orOTZUj?p=preview
What is the motivation / use case for changing the behavior? I’m dynamiclly generating forms from given JSON, so binding to [type] can save a lot of copy-paste code.
Please tell us about your environment: Not sure this is relevant as it is reproducible in plnkr.
Thanks.
Edit: same goes if binding [type] to inputType="checkbox".
Edit2: this is also broken in the Dynamic Forms demo if you change “email” type to number (for example)
About this issue
- Original URL
- State: open
- Created 10 years ago
- Reactions: 92
- Comments: 44 (2 by maintainers)
Giving this a bump because I think it’s still an important issue to fix.
No wonder… why the entire World is changing for React.
Wonder why this still hasn’t been fixed. It seems like a common use case and see many requests to have this support.
2024 hello
React ftw
Hello 2023
Still doesn’t work - 06.12.2022 6 years lmao
Amazing how this “enterprise” framework can’t get forms right, even after 17 versions (some of them with breaking changes, so there is no backward compatibility excuse).
In what sane world is
different from
?
It’s 2022 already. Is this gonna be noticed anytime this year?
Not fixed since 2016? Seriously?
Until this is fixed, you can set a [ngSwitch] on a parent element to the variable holding the input type, and use multiple input tags each with *ngSwitchCase. Not as clean, but it works.
Not fixed?
It’s very surprising, I guess not many use reactive forms or were smart enough not to use an incomplete “feature”
Hello, @alxhub when are you going to fix this important issue?
I don’t understand why this error is labeled with “feature”, “feature under consideration” and “state:blocked”. Seven years ago Dzmitry Shylovich pointed out that it is blocked due to #11716. But that one is completed because they wanted to “implement something along #8785 instead”. But #8785 is locked due to inactivity. So how can we make progress on this one?
@PedroFerr It is rather sad.
This is what the workaround looks like in my code. It’s a bit verbose.
April 2022, still no fix in sight?
This problem has been existed for four years
Still an issue with Angular 9…
🔬 Minimal Reproduction
https://stackblitz.com/edit/angular-number-input-bug
Yeah this is pretty gross. I just ended up rendering differently with control flow instead.
@if(column.type === 'checkbox'){ <input type="checkbox" [formControlName]="column.field" /> } @else { <input [type]="column.type" [formControlName]="column.field" /> }Created a new repro with Angular 18, including Zoneless, esbuild, standalone and OnPush detection. Hopefully this gets us further. Also added my select issue.
It wasn’t locked due to inactivity, it implements through
hostDirective. Def. agree that Angular-Team can now continue on this/forms again. Stumbled over this due to select[multiple] does not work properly; return value will always be an array etc.@michael-letcher Could you please provide an example of code with WA?
I can find only this workaround, but it is working without of changing
ngModel:Here is live example: https://stackblitz.com/edit/angular-checkbox-2way-binding