Skip to content

Commit 98e8fdf

Browse files
garrettldthePunderWoman
authored andcommitted
fix(core): fix Self flag inside embedded views with custom injectors (#50270)
When an embedded view injector is present anywhere above a node in the tree, the `Self` flag was effectively ignored. With this change, embedded view injectors are not checked at all when the `Self` flag is present, because resolution should stop at the current node before reaching any embedded view injector(s). Fixes #49959 PR Close #50270
1 parent f3a13c2 commit 98e8fdf

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

‎packages/core/src/render3/di.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,10 @@ export function getOrCreateInjectable<T>(
399399
if (tNode !== null) {
400400
// If the view or any of its ancestors have an embedded
401401
// view injector, we have to look it up there first.
402-
if (lView[FLAGS] & LViewFlags.HasEmbeddedViewInjector) {
402+
if (lView[FLAGS] & LViewFlags.HasEmbeddedViewInjector &&
403+
// The token must be present on the current node injector when the `Self`
404+
// flag is set, so the lookup on embedded view injector(s) can be skipped.
405+
!(flags & InjectFlags.Self)) {
403406
const embeddedInjectorValue =
404407
lookupTokenUsingEmbeddedInjector(tNode, lView, token, flags, NOT_FOUND);
405408
if (embeddedInjectorValue !== NOT_FOUND) {

‎packages/core/test/acceptance/di_spec.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4517,6 +4517,38 @@ describe('di', () => {
45174517
expect(fixture.componentInstance.menu.tokenValue).toBe('hello');
45184518
});
45194519

4520+
it('should check only the current node with @Self when providing an injection token through an embedded view injector',
4521+
() => {
4522+
@Directive({selector: 'menu'})
4523+
class Menu {
4524+
constructor(@Inject(token) @Self() @Optional() public tokenValue: string) {}
4525+
}
4526+
4527+
@Component({
4528+
template: `
4529+
<menu-trigger [triggerFor]="menuTemplate"></menu-trigger>
4530+
<ng-template #menuTemplate>
4531+
<menu></menu>
4532+
</ng-template>
4533+
`,
4534+
providers: [{provide: token, useValue: 'root'}]
4535+
})
4536+
class App {
4537+
@ViewChild(MenuTrigger) trigger!: MenuTrigger;
4538+
@ViewChild(Menu) menu!: Menu;
4539+
}
4540+
4541+
TestBed.configureTestingModule({declarations: [App, MenuTrigger, Menu]});
4542+
const injector = Injector.create({providers: [{provide: token, useValue: 'hello'}]});
4543+
const fixture = TestBed.createComponent(App);
4544+
fixture.detectChanges();
4545+
4546+
fixture.componentInstance.trigger.open(injector);
4547+
fixture.detectChanges();
4548+
4549+
expect(fixture.componentInstance.menu.tokenValue).toBeNull();
4550+
});
4551+
45204552
it('should be able to provide an injection token to a nested template through a custom injector',
45214553
() => {
45224554
@Directive({selector: 'menu'})

0 commit comments

Comments
 (0)