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.

Custom form control (ControlValueAccesor) doesn't make parent run change detection

See original GitHub issue

🐞 bug report

Affected Package

angular/forms

Description

I’ve created a custom file dropzone which should work correctly with ReactiveFormsModule. The problem is that after I’ve added a file, the control’s parent (the form component) isn’t being updated. According to the valueChanges Observable, the value is being emitted correctly.

<form
    (ngSubmit)="onSubmit()"
    [formGroup]="form"
>
    {{ form.value.file }} <-- this isn't updated until I drop a second file, then it always shows the previous file
<div
        fileDropzone
        formControlName="file"
    ></div>
</form>

I’ve noticed that if I add a custom event on the control, everything works correctly, but I shouldn’t have to do that.

<form
    (ngSubmit)="onSubmit()"
    [formGroup]="form"
>
    {{ form.value.file }} <-- This updates correctly when listening to any custom event on the control
<div
        fileDropzone
        (changed)="onChange($event)"
        formControlName="file"
    ></div>
</form>

I assumed the problem might be OnPush change detection, but even after removing it, the problem persists.

🌍 Your Environment

Angular Version:




Angular CLI: 7.3.8
Node: 8.10.0
OS: win32 x64
Angular: 7.2.13
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

Package                            Version
------------------------------------------------------------
@angular-devkit/architect          0.13.8
@angular-devkit/build-angular      0.13.8
@angular-devkit/build-optimizer    0.13.8
@angular-devkit/build-webpack      0.13.8
@angular-devkit/core               7.3.8
@angular-devkit/schematics         7.3.8
@angular/cdk                       7.3.7
@angular/cli                       7.3.8
@angular/flex-layout               7.0.0-beta.24
@angular/material                  7.3.7
@angular/material-moment-adapter   7.3.7
@ngtools/webpack                   7.3.8
@schematics/angular                7.3.8
@schematics/update                 0.13.8
rxjs                               6.4.0
typescript                         3.2.2
webpack                            4.29.0

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
trotylcommented, Jul 10, 2019
0reactions
angular-automatic-lock-bot[bot]commented, Sep 15, 2019

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

Custom form control couldn't get the right value from onPush ...
In ngmodel directive, setValue is called in next micro-task queue. So the parent component and the ngmodel component are not in one dirty...
Read more >
How to detect changes in the bound form variable value in a ...
I've tried implementing the OnChanges interface, but the SimpleChanges object received doesn't have any reference to the bound form variable.
Read more >
Never again be confused when implementing ... - InDepth.Dev
So, Angular needs a generic mechanism to stand between Angular's formControl and a native/custom form control. This is where the ControlValueAccessor objectΒ ...
Read more >
Creating custom form controls in Angular (Control Value ...
This video covers Control Value Accessors, and how we can use them to minimise boilerplate markup in our forms.
Read more >
Don't reinvent the wheel when implementing ... - Medium
Conclusion. If we want to make a custom form component that's compatible with Reactive Form, we need to implement ControlValueAccessor . HoweverΒ ...
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