forked from angular/angular
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): ChangeDetectorRef.detectChanges should detect bootstrap a…
…nd onPush not dirty component Close angular#36667 Calling `componentRef.detectChanges()` sometimes not work as expected. ``` @component({ selector: 'dynamic', template: `{{name}}`, changeDetection: ChangeDetectionStrategy.OnPush }) export class Dynamic { name = 'initial name'; } const componetRef = cfr.createComponent(Dynamic); componentRef.changeDetectorRef // host view componentRef.injector.get(ChangeDetectorRef) // component view componentRef.instance.cdRef // componentView componentRef.instance.name = 'something new'; // componentRef.instance.cdRef.markForCheck(); componentRef.detectChanges(); // does not work without `markForCheck` componentRef.changeDetectorRef.detectChanges() // Users expect that Bar gets refreshed. componentRef.injector.get(ChangeDetectorRef).detectChanges() componentRef.instance.cdRef.detectChanges() // Example: https://stackblitz.com/edit/angular-drqzhw?file=src%2Fapp%2Fapp.module.ts ngOnInit() { const cmptRef = this._cfr .resolveComponentFactory(Dynamic) .create(this._injector); this._vcRef.insert(cmptRef.hostView); setTimeout(() => { cmptRef.instance.name = "changed name"; console.log("changing name"); cmptRef.changeDetectorRef.detectChanges(); // does not work }); setTimeout(() => { cmptRef.instance.name = "second changed name"; console.log("changing name"); cmptRef.injector.get(ChangeDetectorRef).detectChanges(); // works }, 2000); } ``` Since when the component is `boostrap` or `dynamic`, we created a hostView to host the component. And the `componentRef.changeDetectorRef` references to that hostView instead of the componentView itself. And when the component is `OnPush` and not dirty, calling `changeDetectorRef.detectChanges()` will bypass the component which is confusing. So this commit mark the root component dirty when calling `changeDetectorRef.detectChanges()` from the root hostView.
- Loading branch information
1 parent
72a00dc
commit f16ecd0
Showing
2 changed files
with
117 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters