angular: Reactive Form change through ControlValueAccessor doesn't update view
I’m submitting a … (check one with “x”)
[x] bug report => search github for a similar issue or PR before submitting
[ ] 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 When updating a value through a form field that uses the formControl directive, the view isn’t updated. When, say, two fields hold the same form control, changing one does NOT change the other.
This does happen when using NgModel, and does happen when calling formControl.setValue(newVal)
Expected behavior Behaviour is expected to be the same in NgModel as when using Reactive FormControls
Minimal reproduction of the problem with instructions http://plnkr.co/edit/LXyo6uHdbEpP3feudYoo?p=preview
What is the motivation / use case for changing the behavior?
The contract custom form fields have when using the ControlValueAccessor
interface is broken. A good example is http://valor-software.com/ng2-bootstrap/#/buttons#radio - which works currently only with NgModel, as it requires updating the view on all relevant buttons on value change.
When using a set of buttons with reactive forms, currently, a (click)
based hack needs to be put in place:
<div class="btn-group">
<label
class="btn btn-primary"
formControlName="privateApplication"
(click)="form.get('privateApplication').setValue(false)"
[btnRadio]="false">
{{'LABEL_PUBLIC' | translate}}
</label>
<label
class="btn btn-primary"
formControlName="privateApplication"
(click)="form.get('privateApplication').setValue(true)"
[btnRadio]="true">
{{'LABEL_PRIVATE' | translate}}
</label>
</div>
Please tell us about your environment: OS Not relevant
-
Angular version: 2.4.1
-
Browser: Chrome 55
-
Language: TypeScript 2
About this issue
- Original URL
- State: open
- Created 7 years ago
- Reactions: 62
- Comments: 57 (2 by maintainers)
For those who came from google with this issue like me, there’s a workaround that saved me:
Hello ? Can you fix major 2 years old bugs ?
This is a total blocker when implementing custom form component.
still present with 7.3.4
@Ledzz solution works, but be aware of the jumping cursor in IE11: Try it: https://plnkr.co/edit/ilRx7V7VfNbzgB2lqC1v?p=preview
I need a input field and a select to work on the same control. Following workaround worked for me:
try it: https://plnkr.co/edit/WaLHOlcNAVbSAH8m0a7o?p=preview
On topic: In my opinion this is an unexpected behaviour, so I see it as a bug not a missing feature.
Almost 4 years for this bug… 👎
I have a stepper in my app and last step is a summary of previous forms. Well I have to use same form groups at the last step, but as it is formcontrols duplication summary is not updated anytime
This worked for me for both a reactive form and a non-reactive one:
The template looks like this:
This is not a bug. When changing a class variable via the writeValue method it is a change which is not detected via zone.js or via an Input change. You need to manually trigger the ChangeDetectorRef detectChanges method to let Angular synchronize the class’s instance properties with the DOM, or use the Renderer2 to change the native element value manually as stated in the docs .
… the problem is still valid for Angular 5.2.4 https://stackblitz.com/edit/mlc-two-inputs-same-formcontrol?file=app%2Fapp.component.html
I’m having the same problem. When I do this from my controller:
With this template:
My custom component that implements ControlValueAccessor, never gets the new value passed to it’s writeValue method. There appear to be no workarounds.
I use a painful hack, maybe you cant use this solution always, I don’t know. template:
<input type="text" (input)="inputEventHandler($event)" #tstInput >
component:@ViewChild('tstInput ') tstInput;
If you only have a subset of form fields you know to be ‘duplicated’ you’re slightly better off using
patchValue
instead.This will probably mitigate issues with browser controls quirks such as the above mentioned IE11 issue.
Still strikes me as bizarre this is the ‘by design’ behavior. I suppose it is a performance issue. Everything in angular magically binds - except for the simplest forms 😕
Sorry, ignore the above. I reread your issue see whats going on here, I don’t know the inner workings overly well but it looks to me like a limitation of Reactive Forms currently not necessarily a bug. Maybe opening a Feature request for this after checking if there isn’t one already
This needs some serious looking. Is there any update on whether or not this is being looked into?
Thought I was really missing something in my app… And decided to make a small repro before going insane.
Turns out it’s a real issue.
If anyone ask for a use case, just imagine a color picker, like the one integrated in Chrome debug tool.
You can either change the color from the input or change it from the color palette. Both are same
formControlName
but with different display 🤷♂️While this works definitely, it’s not optimal as Angular needs another ChangeDetection cycle to pick up your changes. Did you try to call
changeDetectorRef.detectChanges()
after the patchValue?Happened to me when I had the following in the
NG_CONTROL_VALUE_ACCESSOR
:commenting out the
.reset()
code solved itI use google search. When I select a place (by enter or by mose’s click), (in code I call FormGroup.patchValue()) Inputs were filled, but view not rendered. View rendered when I do something with browser, even just do click.
angular: v. 5.2.9
please look to attached screens
{emitEvent: false}
does not help me…There are two points to note about the following code: http://plnkr.co/edit/LXyo6uHdbEpP3feudYoo?p=preview
First, it’s worth to say that in this example: there is a subtle difference between the template driven and reactive versions:
We can avoid this difference by wrapping the inputs in a form.
Second, the two versions (template driven and reactive) are not really equivalent. And the problem (if it’s really a problem), exists also in the template driven forms as in the reactive ones. Here http://plnkr.co/edit/mPDLG7MZlp6KRvkYBN89?p=preview I give the equivalent reactive version of the template driven one and the template driven version equivalent to the reactive one.