René's URL Explorer Experiment


Title: fix(core): dynamic onPush component should trigger change detection correctly by JiaLiPassion · Pull Request #39463 · angular/angular · GitHub

Open Graph Title: fix(core): dynamic onPush component should trigger change detection correctly by JiaLiPassion · Pull Request #39463 · angular/angular

X Title: fix(core): dynamic onPush component should trigger change detection correctly by JiaLiPassion · Pull Request #39463 · angular/angular

Description: Close #36667, #39524, #39545. dynamic and bootstrap components has a parent host view as the placeholder. And also all the hooks are registered to the tView of the host view, not the component view. Running change detection from the host view has several issues. componentRef.changeDetectorRef.detectChanges()/markForCheck() will not update the component. Since componentRef.changeDetectorRef is the RootViewRef of the hostView. @component({ selector: 'dynamic', template: `{{name}}`, changeDetection: ChangeDetectionStrategy.OnPush }) export class Dynamic { name = 'initial name'; } @component({ selector: 'my-app', template: ` Expected: "initial name" changes to "changed name" after 2s
Actual:
`, changeDetection: ChangeDetectionStrategy.OnPush }) export class AppComponent { constructor(private _vcRef: ViewContainerRef, private _cfr: ComponentFactoryResolver, private _injector: Injector) {} 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 is the RootViewRef of the hostView cmptRef.changeDetectorRef.detectChanges(); }, 2000); } } The hooks (such as ngOnCheck) is executed, but the component is not updated. Since the hooks are registered to the tView of the hostView. import { Component, NgModule, ViewContainerRef, ComponentFactoryResolver, Injector, ChangeDetectionStrategy, DoCheck } from "@angular/core"; import { BrowserModule } from "@angular/platform-browser"; import { timer } from "rxjs"; @component({ selector: "dynamic", template: ` {{ name }} `, changeDetection: ChangeDetectionStrategy.OnPush }) export class Dynamic implements DoCheck { name = "initial name"; ngDoCheck() { console.log("docheck dynamic"); } } @component({ selector: "my-app", template: ` Expected: "initial name" changes to "changed name" after 2s
Actual:
`, changeDetection: ChangeDetectionStrategy.OnPush }) export class AppComponent { constructor( private _vcRef: ViewContainerRef, private _cfr: ComponentFactoryResolver, private _injector: Injector ) {} 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(); }, 2000); } } @NgModule({ imports: [BrowserModule], declarations: [AppComponent, Dynamic], entryComponents: [Dynamic], bootstrap: [AppComponent] }) export class AppModule {} dev mode will only trigger change detection once for the onPush component. @Component({ template: `

{{increment('componentView')}}
`, changeDetection: ChangeDetectionStrategy.OnPush }) class App { increment() { counters++; } noop() {} } const fixture = TestBed.createComponent(App); fixture.detectChanges(); expect(counters).toEqual(2); // current behavior is 1 The reason is App is a OnPush component with a hostView, calling fixture.detectChanges() will run a detectChanges() and checkNoChanges() . And the App component is onPush, so the checkNoChanges() run will ignore App component, so the counters will be 1 Which is not correct. it('works', () => { @Component({ template: '{{message}}', changeDetection: ChangeDetectionStrategy.OnPush }) class OnPushCmp { @Input() message = 'initial'; ngAfterViewInit() { this.message = 'updated'; } } const comp = TestBed.configureTestingModule({declarations: [OnPushCmp]}).createComponent(OnPushCmp); expect(() => comp.detectChanges()).toThrow(); // current behavior is not throw. }); The reason is the same, the checkNoChanges() will skip the OnPushCmp, so the ExpressionChanged error is not thrown. This PR update several points. When create a dynamic component, we create a host view and a component view. We consider these 2 views a single unit, if the component is OnPush, we set the Dirty flag to the host view to make it a an OnPush View, and set the CheckAlways flag to the component. About handling OnPush in checkNoChanges(), the current behavior is OnPush component will not execute the checkNoChanges() since the Dirty flag is cleared in the detectChanges(), so the ExpressionChanged error will not be thrown for OnPush component, which is a bug, but to fix it, it will be a breaking change for the existing apps, so this PR also add some logic to keep the current buggy behavior. so I also created another commit to add a isOnPushCheckNoChangesEnabled option to enable checkNoChanges for the OnPush component 4dcf5bd

Open Graph Description: Close #36667, #39524, #39545. dynamic and bootstrap components has a parent host view as the placeholder. And also all the hooks are registered to the tView of the host view, not the component view...

X Description: Close #36667, #39524, #39545. dynamic and bootstrap components has a parent host view as the placeholder. And also all the hooks are registered to the tView of the host view, not the component view...

Opengraph URL: https://github.com/angular/angular/pull/39463

X: @github

direct link

Domain: github.com

react-profiling0
react-import-mapreact,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322
route-pattern/:user_id/:repository/pull/:id/files(.:format)
route-controllerpull_requests
route-actionfiles
fetch-noncev2:95936b41-f818-cc67-810d-d19a3edad2ab
current-catalog-service-hashae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b
request-idE4BE:3F4253:DC653A:1262B17:6B0104C9
html-safe-nonce22fe998efc121cf7b98617917b5cf1ca00ffc3a4171ecf9f3ce332e5f46763b6
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFNEJFOjNGNDI1MzpEQzY1M0E6MTI2MkIxNzo2QjAxMDRDOSIsInZpc2l0b3JfaWQiOiI0OTM0ODA4MTM5NDU2MzgyMTUzIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmac12b05897dad6b34a6ba950ce1627bede4272f6d15c7721da51ed5f892d70ad59
hovercard-subject-tagpull_request:511153446
github-keyboard-shortcutsrepository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///pull_requests/show/files
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/angular/angular/pull/39463/files
twitter:imagehttps://avatars.githubusercontent.com/u/1442575?s=400&v=4
twitter:cardsummary_large_image
og:imagehttps://avatars.githubusercontent.com/u/1442575?s=400&v=4
og:image:altClose #36667, #39524, #39545. dynamic and bootstrap components has a parent host view as the placeholder. And also all the hooks are registered to the tView of the host view, not the component view...
og:site_nameGitHub
og:typeobject
hostnamegithub.com
expected-hostnamegithub.com
Nonecb7fa109eca7d4156c889f474306b0e91336055e9adf8fd4684b61e49094d124
turbo-cache-controlno-preview
diff-viewunified
go-importgithub.com/angular/angular git https://github.com/angular/angular.git
octolytics-dimension-user_id139426
octolytics-dimension-user_loginangular
octolytics-dimension-repository_id24195339
octolytics-dimension-repository_nwoangular/angular
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id24195339
octolytics-dimension-repository_network_root_nwoangular/angular
turbo-body-classeslogged-out env-production page-responsive full-width
disable-turbotrue
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release2b59d31a4e44f08b7af67a4b67f8fd0af3befe2a
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/angular/angular/pull/39463/files#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fangular%2Fangular%2Fpull%2F39463%2Ffiles
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fangular%2Fangular%2Fpull%2F39463%2Ffiles
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fpull_requests%2Fshow%2Ffiles&source=header-repo&source_repo=angular%2Fangular
Reloadhttps://github.com/angular/angular/pull/39463/files
Reloadhttps://github.com/angular/angular/pull/39463/files
Reloadhttps://github.com/angular/angular/pull/39463/files
angularhttps://github.com/angular
angularhttps://github.com/angular/angular
Codehttps://github.com/angular/angular
Issues835 (835)https://github.com/angular/angular/issues
Pull requests141 (141)https://github.com/angular/angular/pulls
Discussionshttps://github.com/angular/angular/discussions
Actionshttps://github.com/angular/angular/actions
Projectshttps://github.com/angular/angular/projects
Security and quality40 (40)https://github.com/angular/angular/security
Insightshttps://github.com/angular/angular/insights
Sign up for GitHub https://github.com/signup?return_to=%2Fangular%2Fangular%2Fissues%2Fnew%2Fchoose
terms of servicehttps://docs.github.com/terms
privacy statementhttps://docs.github.com/privacy
Sign inhttps://github.com/login?return_to=%2Fangular%2Fangular%2Fissues%2Fnew%2Fchoose
JiaLiPassionhttps://github.com/JiaLiPassion
angular:masterhttps://github.com/angular/angular/tree/master
JiaLiPassion:root-comp-cdrhttps://github.com/JiaLiPassion/angular/tree/root-comp-cdr
Conversation 9 https://github.com/angular/angular/pull/39463
Commits 5 https://github.com/angular/angular/pull/39463/commits
Checks 0 https://github.com/angular/angular/pull/39463/checks
Files changed https://github.com/angular/angular/pull/39463/files
Please reload this pagehttps://github.com/angular/angular/pull/39463/files
fix(core): dynamic onPush component should trigger change detection correctly https://github.com/angular/angular/pull/39463/files#top
Show all changes 5 commits https://github.com/angular/angular/pull/39463/files
6003457 fix(core): dynamic onPush component should trigger change detection c… JiaLiPassion Nov 2, 2020 https://github.com/angular/angular/pull/39463/commits/6003457a73e1f4372b27556bbfea3a6e8cbdce70
a6d60ba test(core): update dynamic onPush component change detection test JiaLiPassion Nov 3, 2020 https://github.com/angular/angular/pull/39463/commits/a6d60ba76be03ba6a3618abb00a9746b015d7488
d5417d8 fix(core): keep onPush not run checkNoChanges behavior JiaLiPassion Nov 3, 2020 https://github.com/angular/angular/pull/39463/commits/d5417d8702cd536ac32dda07794bc8f91902d1e1
8421b26 test(core): update golden symbols JiaLiPassion Nov 3, 2020 https://github.com/angular/angular/pull/39463/commits/8421b26b5b3915bdb10c5773789b6e9c3caabdaa
9de4d3d feat(core): add isOnPushCheckNoChangesEnabled option JiaLiPassion Nov 4, 2020 https://github.com/angular/angular/pull/39463/commits/9de4d3d7ecebab47f3846f6d5e090a3782f4e191
Clear filters https://github.com/angular/angular/pull/39463/files
Please reload this pagehttps://github.com/angular/angular/pull/39463/files
Please reload this pagehttps://github.com/angular/angular/pull/39463/files
application_ref.ts https://github.com/angular/angular/pull/39463/files#diff-c3bc2f4d2923398d46897c1233ab19cec2611e44ea60592e7e0d5ed03747a897
component.ts https://github.com/angular/angular/pull/39463/files#diff-dcda220a37338462837f5abe90db2b576fca57f34b40f36820514cdc696e097d
shared.ts https://github.com/angular/angular/pull/39463/files#diff-92e488b29fcb339dca89bcc326131e4e7482ceac84387f5e5250c3441a9dd326
state.ts https://github.com/angular/angular/pull/39463/files#diff-944eb46933429dc12ec1d68ccb1069562a3bb2c749a2ceb2ac532ff9863e5edf
change_detection_spec.ts https://github.com/angular/angular/pull/39463/files#diff-25d932b0d3748a7363626694f5f47c899c0d2de76ce0b209db4a3403ebad182e
bundle.golden_symbols.json https://github.com/angular/angular/pull/39463/files#diff-682c41ab8c9c211b98fdfd11a6864d3911267eaf1b4c9c39b94bac0f6e712661
bundle.golden_symbols.json https://github.com/angular/angular/pull/39463/files#diff-064ba24c47aae98e5c835feff80eb65f7f89ec7493eeea6223165aee99113f3a
bundle.golden_symbols.json https://github.com/angular/angular/pull/39463/files#diff-b45ad5203dc5e93dba1c03c72c197220625be52c2bac940397a5b62179ec49dd
bundle.golden_symbols.json https://github.com/angular/angular/pull/39463/files#diff-0ea07045c567fb07c48994161c0a3bfa5bd1a676bc484d0fd8d3f9c5c2bc5d8d
bundle.golden_symbols.json https://github.com/angular/angular/pull/39463/files#diff-c8a15946b2828a34229ac187045402b1ea37f0cb736063560a2a20f2744c23fc
packages/core/src/application_ref.tshttps://github.com/angular/angular/pull/39463/files#diff-c3bc2f4d2923398d46897c1233ab19cec2611e44ea60592e7e0d5ed03747a897
View file https://github.com/JiaLiPassion/angular/blob/9de4d3d7ecebab47f3846f6d5e090a3782f4e191/packages/core/src/application_ref.ts
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/angular/angular/pull/39463/{{ revealButtonHref }}
https://github.com/angular/angular/pull/39463/files#diff-c3bc2f4d2923398d46897c1233ab19cec2611e44ea60592e7e0d5ed03747a897
https://github.com/angular/angular/pull/39463/files#diff-c3bc2f4d2923398d46897c1233ab19cec2611e44ea60592e7e0d5ed03747a897
https://github.com/angular/angular/pull/39463/files#diff-c3bc2f4d2923398d46897c1233ab19cec2611e44ea60592e7e0d5ed03747a897
atscotthttps://github.com/atscott
Nov 4, 2020https://github.com/angular/angular/pull/39463/files#r517519154
Learn morehttps://docs.github.com/articles/managing-disruptive-comments/#hiding-a-comment
Please reload this pagehttps://github.com/angular/angular/pull/39463/files
atscotthttps://github.com/atscott
Nov 4, 2020https://github.com/angular/angular/pull/39463/files#r517556516
Please reload this pagehttps://github.com/angular/angular/pull/39463/files
Learn morehttps://docs.github.com/articles/managing-disruptive-comments/#hiding-a-comment
presubmithttps://test.corp.google.com/ui#id=OCL:340242567:BASE:340666852:1604511736133:24f3550
Please reload this pagehttps://github.com/angular/angular/pull/39463/files
JiaLiPassionhttps://github.com/JiaLiPassion
Nov 4, 2020https://github.com/angular/angular/pull/39463/files#r517560204
Learn morehttps://docs.github.com/articles/managing-disruptive-comments/#hiding-a-comment
@atscotthttps://github.com/atscott
Please reload this pagehttps://github.com/angular/angular/pull/39463/files
https://github.com/angular/angular/pull/39463/files#diff-c3bc2f4d2923398d46897c1233ab19cec2611e44ea60592e7e0d5ed03747a897
https://github.com/angular/angular/pull/39463/files#diff-c3bc2f4d2923398d46897c1233ab19cec2611e44ea60592e7e0d5ed03747a897
https://github.com/angular/angular/pull/39463/files#diff-c3bc2f4d2923398d46897c1233ab19cec2611e44ea60592e7e0d5ed03747a897
packages/core/src/render3/component.tshttps://github.com/angular/angular/pull/39463/files#diff-dcda220a37338462837f5abe90db2b576fca57f34b40f36820514cdc696e097d
View file https://github.com/JiaLiPassion/angular/blob/9de4d3d7ecebab47f3846f6d5e090a3782f4e191/packages/core/src/render3/component.ts
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/angular/angular/pull/39463/{{ revealButtonHref }}
https://github.com/angular/angular/pull/39463/files#diff-dcda220a37338462837f5abe90db2b576fca57f34b40f36820514cdc696e097d
https://github.com/angular/angular/pull/39463/files#diff-dcda220a37338462837f5abe90db2b576fca57f34b40f36820514cdc696e097d
packages/core/src/render3/instructions/shared.tshttps://github.com/angular/angular/pull/39463/files#diff-92e488b29fcb339dca89bcc326131e4e7482ceac84387f5e5250c3441a9dd326
View file https://github.com/JiaLiPassion/angular/blob/9de4d3d7ecebab47f3846f6d5e090a3782f4e191/packages/core/src/render3/instructions/shared.ts
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/angular/angular/pull/39463/{{ revealButtonHref }}
https://github.com/angular/angular/pull/39463/files#diff-92e488b29fcb339dca89bcc326131e4e7482ceac84387f5e5250c3441a9dd326
https://github.com/angular/angular/pull/39463/files#diff-92e488b29fcb339dca89bcc326131e4e7482ceac84387f5e5250c3441a9dd326
https://github.com/angular/angular/pull/39463/files#diff-92e488b29fcb339dca89bcc326131e4e7482ceac84387f5e5250c3441a9dd326
https://github.com/angular/angular/pull/39463/files#diff-92e488b29fcb339dca89bcc326131e4e7482ceac84387f5e5250c3441a9dd326
https://github.com/angular/angular/pull/39463/files#diff-92e488b29fcb339dca89bcc326131e4e7482ceac84387f5e5250c3441a9dd326
https://github.com/angular/angular/pull/39463/files#diff-92e488b29fcb339dca89bcc326131e4e7482ceac84387f5e5250c3441a9dd326
https://github.com/angular/angular/pull/39463/files#diff-92e488b29fcb339dca89bcc326131e4e7482ceac84387f5e5250c3441a9dd326
packages/core/src/render3/state.tshttps://github.com/angular/angular/pull/39463/files#diff-944eb46933429dc12ec1d68ccb1069562a3bb2c749a2ceb2ac532ff9863e5edf
View file https://github.com/JiaLiPassion/angular/blob/9de4d3d7ecebab47f3846f6d5e090a3782f4e191/packages/core/src/render3/state.ts
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/angular/angular/pull/39463/{{ revealButtonHref }}
https://github.com/angular/angular/pull/39463/files#diff-944eb46933429dc12ec1d68ccb1069562a3bb2c749a2ceb2ac532ff9863e5edf
https://github.com/angular/angular/pull/39463/files#diff-944eb46933429dc12ec1d68ccb1069562a3bb2c749a2ceb2ac532ff9863e5edf
https://github.com/angular/angular/pull/39463/files#diff-944eb46933429dc12ec1d68ccb1069562a3bb2c749a2ceb2ac532ff9863e5edf
https://github.com/angular/angular/pull/39463/files#diff-944eb46933429dc12ec1d68ccb1069562a3bb2c749a2ceb2ac532ff9863e5edf
Please reload this pagehttps://github.com/angular/angular/pull/39463/files
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.