Title: @SkipSelf() cd: ChangeDetectorRef doesn't return parent ChangeDetector · Issue #39475 · angular/angular · GitHub
Open Graph Title: @SkipSelf() cd: ChangeDetectorRef doesn't return parent ChangeDetector · Issue #39475 · angular/angular
X Title: @SkipSelf() cd: ChangeDetectorRef doesn't return parent ChangeDetector · Issue #39475 · angular/angular
Description: 🐞 bug report Affected Package @angular/core/di Is this a regression? I'm not sure, but given this workaround for bug #22560 it probably worked pre-Ivy. Description It's not possible for a component to obtain the ChangeDetectorRef or Inje...
Open Graph Description: 🐞 bug report Affected Package @angular/core/di Is this a regression? I'm not sure, but given this workaround for bug #22560 it probably worked pre-Ivy. Description It's not possible for a component...
X Description: 🐞 bug report Affected Package @angular/core/di Is this a regression? I'm not sure, but given this workaround for bug #22560 it probably worked pre-Ivy. Description It's not possible for a c...
Opengraph URL: https://github.com/angular/angular/issues/39475
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"@SkipSelf() cd: ChangeDetectorRef doesn't return parent ChangeDetector","articleBody":"# 🐞 bug report\r\n\r\n### Affected Package\r\n@angular/core/di\r\n\r\n### Is this a regression?\r\nI'm not sure, but given [this workaround](https://github.com/angular/angular/issues/22560#issuecomment-473958144) for bug #22560 it probably worked pre-Ivy.\r\n\r\n### Description\r\n\r\nIt's not possible for a component to obtain the `ChangeDetectorRef` or `Injector` from its parent by using @SkipSelf. eg:\r\n\r\n```ts\r\n constructor(\r\n // Using parent ChangeDetector, b/c host bindings are part of the parent CD.\r\n // Not intuitive, but true: https://github.com/angular/angular/issues/22560\r\n @SkipSelf() changeDetector: ChangeDetectorRef\r\n ) {\r\n```\r\n\r\nThere is no compile error or runtime error, but the argument passed in is the component's ChangeDetectorRef or Injector.\r\n\r\nThe bug is apparent in `getOrCreateInjectable()`:\r\n```ts\r\nexport function getOrCreateInjectable\u003cT>(\r\n tNode: TDirectiveHostNode|null, lView: LView, token: Type\u003cT>|InjectionToken\u003cT>,\r\n flags: InjectFlags = InjectFlags.Default, notFoundValue?: any): T|null {\r\n if (tNode !== null) {\r\n const bloomHash = bloomHashBitOrFactory(token);\r\n // If the ID stored here is a function, this is a special object like ElementRef or TemplateRef\r\n // so just call the factory function to create it.\r\n if (typeof bloomHash === 'function') {\r\n enterDI(lView, tNode);\r\n try {\r\n const value = bloomHash();\r\n if (value == null && !(flags & InjectFlags.Optional)) {\r\n throw new Error(`No provider for ${stringifyForError(token)}!`);\r\n } else {\r\n return value;\r\n }\r\n } finally {\r\n leaveDI();\r\n }\r\n } else if (typeof bloomHash == 'number') {\r\n if (bloomHash === -1) {\r\n // `-1` is a special value used to identify `Injector` types.\r\n return new NodeInjector(tNode, lView) as any;\r\n }\r\n```\r\n\r\nIf the injection token is one of (ChangeDetectorRef, Injector, ElementRef, ViewContainerRef, TemplateRef) it has a `NG_ELEMENT_ID` that is a function, which results in all `InjectFlags` being ignored except `InjectFlags.Optional`.\r\n\r\nIn combination with this bug ( [InjectFlags ignored by NodeInjector.get()](https://github.com/angular/angular/issues/31776#issuecomment-717398208) ) and this by-design bug ( [HostBindings not updated by detectChanges() in OnPush component](https://github.com/angular/angular/issues/22560) ), there's no reasonable way to update HostBindings in OnPush components.\r\n\r\nI think it's reasonable that some of the unsupported injection tokens don't support `@SkipSelf()` (eg TemplateRef and ViewContainerRef make sense; Injector is debatable), but due to HostBindings only being updated by the parent ChangeDetector, we need a straightforward way to obtain the parent ChangeDetector. Also, if `@SkipSelf()` (or other flags) aren't supported, an error should be raised (compile time or runtime) instead of injecting the wrong argument.\r\n\r\n## 🔬 Minimal Reproduction\r\nThis is a karma test running in StackBlitz\r\nhttps://stackblitz.com/edit/ng10-bug-skipself\r\n\r\nTest source:\r\n```ts\r\nimport { ChangeDetectorRef, Component, Injectable, Injector, SkipSelf } from '@angular/core';\r\nimport { ComponentFixture, TestBed } from '@angular/core/testing';\r\n\r\n@Injectable()\r\nclass TestService {\r\n constructor(public id: string) { }\r\n}\r\n\r\n@Component({\r\n selector: 'app-child',\r\n template: `\u003cdiv>nothing to see here\u003c/div>`,\r\n providers: [\r\n {\r\n provide: TestService,\r\n useFactory: () => new TestService('child')\r\n }\r\n ]\r\n})\r\nclass ChildComponent {\r\n\r\n constructor(\r\n public changeDetector: ChangeDetectorRef,\r\n @SkipSelf() public parentChangeDetector: ChangeDetectorRef,\r\n public injector: Injector,\r\n @SkipSelf() public parentInjector: Injector,\r\n @SkipSelf() public testService: TestService\r\n\r\n ) { }\r\n\r\n}\r\n\r\n@Component({\r\n selector: 'app-parent',\r\n template: `\u003capp-child>\u003c/app-child>`,\r\n providers: [\r\n {\r\n provide: TestService,\r\n useFactory: () => new TestService('parent')\r\n }\r\n ]\r\n})\r\nclass ParentComponent {\r\n\r\n constructor(\r\n public changeDetector: ChangeDetectorRef,\r\n public injector: Injector\r\n ) { }\r\n\r\n}\r\n\r\ndescribe('Component injection with @SkipSelf', () => {\r\n\r\n let fixture: ComponentFixture\u003cParentComponent>;\r\n let child: ChildComponent;\r\n\r\n beforeEach((() => {\r\n TestBed.configureTestingModule({\r\n declarations: [\r\n ChildComponent,\r\n ParentComponent\r\n ],\r\n }).compileComponents();\r\n\r\n fixture = TestBed.createComponent(ParentComponent);\r\n child = fixture.debugElement.children[0].componentInstance as ChildComponent;\r\n }));\r\n\r\n it('child can get parent Injector (FAILS)', () => {\r\n expect((child.parentInjector as any)._lView)\r\n .toBe((fixture.componentInstance.injector as any)._lView);\r\n });\r\n\r\n it('child can get parent ChangeDetector (FAILS)', () => {\r\n expect((child.parentChangeDetector as any)._cdRefInjectingView)\r\n .toBe((fixture.componentInstance.changeDetector as any)._cdRefInjectingView);\r\n });\r\n\r\n it('inject(@SkipSelf Injector) is different from inject(Injector) (FAILS)', () => {\r\n expect((child.parentInjector as any)._lView)\r\n .not.toBe((child.injector as any)._lView);\r\n });\r\n\r\n it('inject(@SkipSelf Injector) returns service from parent (FAILS)', () => {\r\n const parentInjectorService = child.parentInjector.get(TestService);\r\n const childInjectorService = child.injector.get(TestService);\r\n expect(parentInjectorService.id)\r\n .not.toBe(childInjectorService.id);\r\n });\r\n\r\n it('inject(@SkipSelf ChangeDetectorRef) is different from inject(ChangeDetectorRef) (FAILS)', () => {\r\n expect((child.parentChangeDetector as any)._cdRefInjectingView)\r\n .not.toBe((child.changeDetector as any)._cdRefInjectingView);\r\n });\r\n\r\n it('inject(@SkipSelf TestService) returns service from parent component (PASSES)', () => {\r\n expect(child.testService.id)\r\n .toBe('parent');\r\n });\r\n\r\n});\r\n```\r\n\r\n## 🌍 Your Environment\r\n\r\n**Angular Version:**\r\n\u003cpre>\u003ccode>\r\nAngular CLI: 10.0.8\r\nNode: 14.8.0\r\nOS: win32 x64\r\n\r\nAngular: 10.0.14\r\n... animations, common, compiler, compiler-cli, core, forms\r\n... platform-browser, platform-browser-dynamic, router\r\nIvy Workspace: Yes\r\n\r\nPackage Version\r\n-----------------------------------------------------------\r\n@angular-devkit/architect 0.1000.8\r\n@angular-devkit/build-angular 0.1000.8\r\n@angular-devkit/build-optimizer 0.1000.8\r\n@angular-devkit/build-webpack 0.1000.8\r\n@angular-devkit/core 10.0.8\r\n@angular-devkit/schematics 10.0.8\r\n@angular/cli 10.0.8\r\n@ngtools/webpack 10.0.8\r\n@schematics/angular 10.0.8\r\n@schematics/update 0.1000.8\r\nrxjs 6.5.5\r\ntypescript 3.9.7\r\nwebpack 4.43.0\r\n\u003c/code>\u003c/pre>\r\n\r\n","author":{"url":"https://github.com/johncrim","@type":"Person","name":"johncrim"},"datePublished":"2020-10-28T18:19:42Z","url":"https://github.com/angular/angular/issues/39475"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:85cd329b-b393-da61-0f3a-b904db344b23 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E1CA:2DBB21:120F83A:180EC3C:6B012131 |
| html-safe-nonce | 289484fc95f874fef51581954fc84266a0b3c4a83db19fad806f05e1a93d9c5c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMUNBOjJEQkIyMToxMjBGODNBOjE4MEVDM0M6NkIwMTIxMzEiLCJ2aXNpdG9yX2lkIjoiOTEyNjQ1MTkxODMxNzYyNTY0OSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | f1433f3ebae2930ae1ad2869c277f64c9aab98ab013713ad1f14f8b49d8fd0e8 |
| hovercard-subject-tag | issue:731683506 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/angular/angular/39475/issue_layout |
| twitter:image | https://opengraph.githubassets.com/be759a8ab50b534bb55459a93aeb8a9cc710aeebb92be4c4677ceaf11eb3093c/angular/angular/issues/39475 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/be759a8ab50b534bb55459a93aeb8a9cc710aeebb92be4c4677ceaf11eb3093c/angular/angular/issues/39475 |
| og:image:alt | 🐞 bug report Affected Package @angular/core/di Is this a regression? I'm not sure, but given this workaround for bug #22560 it probably worked pre-Ivy. Description It's not possible for a component... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | johncrim |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/angular/angular git https://github.com/angular/angular.git |
| octolytics-dimension-user_id | 139426 |
| octolytics-dimension-user_login | angular |
| octolytics-dimension-repository_id | 24195339 |
| octolytics-dimension-repository_nwo | angular/angular |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24195339 |
| octolytics-dimension-repository_network_root_nwo | angular/angular |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width