Repository navigation
Description
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
- Change detection executes for parent component.
- Change detection updates [ngModel] value
- [ngModel] schedules async task that updates child component
- Change detection executes for child component
- 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
kara commentedon Oct 17, 2016
Related:
#9782
#11563
andreialecu commentedon Nov 2, 2016
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
ngModelbecause of this and have to resort to avoiding ControlValueAccesor and using separate explicit@Inputand@Outputproperties with a different name thanngModel.Another good repro is available here: #12607
bennadel commentedon Nov 4, 2016
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.ilianiv commentedon Nov 14, 2016
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.
eckdanny commentedon Dec 19, 2016
I think I can get around this by subscribing to
statusChangesoutside the validator:andreialecu commentedon Jan 14, 2017
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
kkovalevskiy commentedon Jan 14, 2017
@kara could you clarify pls, why
ngModelshould 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.kkovalevskiy commentedon Jan 16, 2017
@DzmitryShylovich thanks for your answer, looks like I got it, but I still have several questions.
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 inng_model.tsthat forces second change detection. All that I see is just the following calling chainngOnChanges -> _updateValue -> <async call> this.control.setValueresolvedPromise-this is a general problem when using exportAs for directives!Probably the original issue is thatexportAsandngModeldon't play well with unidirectinal dataflow? Maybe in this example we should use some@Outputproperty (validChange) instead of<div>{{myModel.valid}}</div>? As I understand conception of unidirectional dataflow,myModel.validis somekind of output propertypetitcl commentedon Mar 12, 2017
Any update on this issue? or a temporary workaround? I need this for my project too.
I have a parent component (with
OnPushstrategy &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).
DzmitryShylovich commentedon Mar 12, 2017
Doesn't look like it will be fixed in the near future.
Don't use
OnPushwith form componentskkovalevskiy commentedon Mar 13, 2017
@freedonaab
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.
25 remaining items
vvolodin commentedon Jul 3, 2020
4 years and still going nowhere with this...
MarkoSulamagi commentedon Mar 22, 2021
Bumping this issue. I just spent hours debugging why change detection isn't working until I found this thread :/
DoanVanThuong commentedon Apr 16, 2021
I spent half a day to find solution, and now, I found this issue on github and it's still opening. hoho
mlc-mlapis commentedon Apr 16, 2021
@DoanVanThuong This is a simple example of
OnPusheffect on CD:https://stackblitz.com/edit/angular-parent-child-onpush?file=src%2Fapp%2Fparent%2Fparent.component.ts
IgorKurkov commentedon Apr 20, 2021
any updates? how to trigger
ngForm.statusChangeswhenonSubmitis fired? I can't tell child components about form submitting that they are invalid (empty) when onPush is enabled.AndrewKushnir commentedon Feb 1, 2022
Closing this issue since PR #44886 is now merged, the fix will be available in v13.2.1 release.
chabb commentedon Feb 4, 2022
@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() )
AndrewKushnir commentedon Feb 4, 2022
@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.
chabb commentedon Feb 5, 2022
@AndrewKushnir I've opened #44976
angular-automatic-lock-bot commentedon Mar 8, 2022
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.