Skip to content

NgModel works incorrectly when using onPush ChangeDetection strategy #10816

Closed

Description

@kkovalevskiy

I'm submitting a ...

[x] bug report - related issue https://github.com/angular/angular/issues/10148, but it's about testing.

Current behavior
I have two components: the first one is a simple child component with some input and basic support of [ngModel] directive via ControlValueAccesor interface. The second one is a parent component has onPush change detection strategy and populates a value into child component via [ngModel] directive (right after that it's marked for changes via ChangeDetectorRef class). When [ngModel] is updated child component still displays an old value in the template while the component object has actual value in target property.

As I can see NgModel directive updates target component asynchronously - https://github.com/angular/angular/blob/2.0.0/modules/%40angular/forms/src/directives/ng_model.ts#L191-L193 so the following scenario occurs

  1. Change detection executes for parent component.
  2. Change detection updates [ngModel] value
  3. [ngModel] schedules async task that updates child component
  4. Change detection executes for child component
  5. Actual value arrives in child component.

Expected/desired behavior
Change detection should work with already updated component to ensure that new value has been populated into UI.

So it looks like that last two steps should be swapped (or even better - [ngModel] can update child component synchronously).

Reproduction of the problem
Here is a tiny example with the issue. I'm expecting here that right after 2 seconds App Component should display "new value".
http://plnkr.co/edit/IabIsT3323GpZmQSPd4z?p=preview
new example can be found here http://plnkr.co/edit/KowgH96EtDbVJao13Hz5?p=preview, thanks to @andreialecu

if I use the following markup in the example above
<my-child [value]="value"></my-child>
instead of
<my-child [ngModel]="value"></my-child>
all work as expected

Please tell us about your environment:

  • Angular version: 2.0.0-rc.05
  • Browser: [ Chrome 52]
  • Language: [TypeScript 1.8 | ES5]

Activity

  1. kara commented on Oct 17, 2016

    @kara
    Contributor

    Related:

    #9782
    #11563

  2. andreialecu commented on Nov 2, 2016

    @andreialecu
    Contributor

    Updated plunker to work with Angular 2.0 final: http://plnkr.co/edit/KowgH96EtDbVJao13Hz5?p=preview Old one wasn't working any more.

    This seems pretty severe, I cannot use ngModel because of this and have to resort to avoiding ControlValueAccesor and using separate explicit @Input and @Output properties with a different name than ngModel.

    Another good repro is available here: #12607

  3. bennadel commented on Nov 4, 2016

    @bennadel

    I would also add explicitly that the ngOnChanges() life-cycle event method should be fired. I didn't see that mentioned in this version of the ticket.

  4. ilianiv commented on Nov 14, 2016

    @ilianiv

    Have the same issue. Cannot use onPush in my components because I use 3rd party components (value accessors) as child components that cannot be pinged outside to markForCheck() themselves and trigger change detection.

  5. eckdanny commented on Dec 19, 2016

    @eckdanny

    I think I can get around this by subscribing to statusChanges outside the validator:

    // When you go from PENDING to anything else :p
    this.myFormCtrl.statusChanges
      .bufferCount(2, 1)
      .map(states => states[0])
      .filter(prevState => prevState === 'PENDING')
      .subscribe(status => {
        this._ref.markForCheck();
      });
  6. andreialecu commented on Jan 14, 2017

    @andreialecu
    Contributor

    This continues to be a severe problem for my project needing ugly workarounds.

    Been reported in August, still no fix in sight? @kara, mind taking a look? This fiddle clearly shows the issue: http://plnkr.co/edit/KowgH96EtDbVJao13Hz5?p=preview

  7. kkovalevskiy commented on Jan 14, 2017

    @kkovalevskiy
    Author

    @kara could you clarify pls, why ngModel should update target controls asynchronously? (https://github.com/angular/angular/blob/2.0.0/modules/%40angular/forms/src/directives/ng_model.ts#L191-L193). If they will be updated synchronously it will resolve the issue.

  8. kkovalevskiy commented on Jan 16, 2017

    @kkovalevskiy
    Author

    @DzmitryShylovich thanks for your answer, looks like I got it, but I still have several questions.

    1. ngModel forces an additional change detection run when its inputs change: probably I didn't understand how it's implemented, but I don't see any code in ng_model.ts that forces second change detection. All that I see is just the following calling chain ngOnChanges -> _updateValue -> <async call> this.control.setValue
    2. Also I see a note in the comment to resolvedPromise - this is a general problem when using exportAs for directives! Probably the original issue is that exportAs and ngModel don't play well with unidirectinal dataflow? Maybe in this example we should use some @Output property (validChange) instead of <div>{{myModel.valid}}</div>? As I understand conception of unidirectional dataflow, myModel.valid is somekind of output property
  9. petitcl commented on Mar 12, 2017

    @petitcl

    Any update on this issue? or a temporary workaround? I need this for my project too.
    I have a parent component (with OnPush strategy & ControlValueAccessor) that wraps a third party component (with default strategy & ControlValueAccessor).
    When the parent component changes the value, the child component is never updated, unless it receives an user event (ie: hover, click).

  10. DzmitryShylovich commented on Mar 12, 2017

    @DzmitryShylovich
    Contributor

    Any update on this issue?

    Doesn't look like it will be fixed in the near future.

    temporary workaround?

    Don't use OnPush with form components

  11. kkovalevskiy commented on Mar 13, 2017

    @kkovalevskiy
    Author

    @freedonaab

    I have a parent component (with OnPush strategy & ControlValueAccessor) that wraps a third party component (with default strategy & ControlValueAccessor).

    Not sure that your issue is related with this one. I'd say that in your case default change detection strategy of child component won't be working automatically if parent component has OnPush change detection strategy. You have to subscribe on events (or observables) of child component and run change detection manually via ChangeDetectorRef in parent component. This happens due to the fact that angular doesn't track child components of OnPush component (see also https://vsavkin.com/change-detection-in-angular-2-4f216b855d4c#.jwoetznkz)

    This issue is just about the fact that embedded forms directives doesn't work with OnPush components and looks like they violate unidirectional dataflow principle - https://vsavkin.com/two-phases-of-angular-2-applications-fda2517604be#.d30zyylv1. This also leads to necessity to run change detection several times, until component doesn't become "stable". Interesting fact that one core part of angular framework doesn't follow its core principles and looks like these directives are working only due to the fact that angular runs change detection very often, even on mouse move.

  12. 25 remaining items

  13. vvolodin commented on Jul 3, 2020

    @vvolodin

    4 years and still going nowhere with this...

  14. added
    P3
    and removed on Oct 1, 2020
  15. MarkoSulamagi commented on Mar 22, 2021

    @MarkoSulamagi

    Bumping this issue. I just spent hours debugging why change detection isn't working until I found this thread :/

  16. DoanVanThuong commented on Apr 16, 2021

    @DoanVanThuong

    I spent half a day to find solution, and now, I found this issue on github and it's still opening. hoho

  17. mlc-mlapis commented on Apr 16, 2021

    @mlc-mlapis
    Contributor
  18. IgorKurkov commented on Apr 20, 2021

    @IgorKurkov

    any updates? how to trigger ngForm.statusChanges when onSubmit is fired? I can't tell child components about form submitting that they are invalid (empty) when onPush is enabled.

  19. AndrewKushnir commented on Feb 1, 2022

    @AndrewKushnir
    Contributor

    Closing this issue since PR #44886 is now merged, the fix will be available in v13.2.1 release.

  20. chabb commented on Feb 4, 2022

    @chabb
    Contributor

    @AndrewKushnir I think #11563 ( which was marked as a related issue ) is still an open issue. Here : https://stackblitz.com/edit/angular-6msgu9?file=src%2Fapp%2Fapp.component.ts
    When you click on the change button, the value is correctly updated, but the ng-invalid class is still present.
    Is this expected ? ( in that case we should use .markForCheck() )

  21. AndrewKushnir commented on Feb 4, 2022

    @AndrewKushnir
    Contributor

    @chabb could you please create a new ticket to describe what the problem is (what's the expected vs actual behavior that you see)? Thank you.

  22. chabb commented on Feb 5, 2022

    @chabb
    Contributor
  23. angular-automatic-lock-bot commented on Mar 8, 2022

    @angular-automatic-lock-bot

    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.

  24. locked and limited conversation to collaborators on Mar 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3area: formsforms: change detectionforms: ngModelfreq2: mediumstate: confirmedtype: bug/fix

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions