Skip to content

fix(cdk/dialog): fall back to node injector token doesn't exist in template injector #25406

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion src/cdk/dialog/dialog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
ChangeDetectionStrategy,
Component,
Directive,
inject,
Inject,
InjectionToken,
Injector,
Expand Down Expand Up @@ -52,8 +53,13 @@ describe('Dialog', () => {
DialogWithInjectedData,
DialogWithoutFocusableElements,
DirectiveWithViewContainer,
TemplateInjectorParentComponent,
TemplateInjectorInnerDirective,
],
providers: [
{provide: Location, useClass: SpyLocation},
{provide: TEMPLATE_INJECTOR_TEST_TOKEN, useValue: 'hello from test module'},
],
providers: [{provide: Location, useClass: SpyLocation}],
});

TestBed.compileComponents();
Expand Down Expand Up @@ -710,6 +716,22 @@ describe('Dialog', () => {

expect(overlayContainerElement.querySelector('cdk-dialog-container')).toBeTruthy();
}));

it(
'should fall back to node injector in template dialog if token does not exist in ' +
'template injector',
fakeAsync(() => {
const templateInjectFixture = TestBed.createComponent(TemplateInjectorParentComponent);
templateInjectFixture.detectChanges();

dialog.open(templateInjectFixture.componentInstance.templateRef);
templateInjectFixture.detectChanges();

expect(templateInjectFixture.componentInstance.innerComponentValue).toBe(
'hello from parent component',
);
}),
);
});

describe('hasBackdrop option', () => {
Expand Down Expand Up @@ -1233,3 +1255,28 @@ class DialogWithoutFocusableElements {}
encapsulation: ViewEncapsulation.ShadowDom,
})
class ShadowDomComponent {}

const TEMPLATE_INJECTOR_TEST_TOKEN = new InjectionToken<string>('TEMPLATE_INJECTOR_TEST_TOKEN');

@Component({
template: `<ng-template><template-injector-inner></template-injector-inner></ng-template>`,
providers: [
{
provide: TEMPLATE_INJECTOR_TEST_TOKEN,
useValue: 'hello from parent component',
},
],
})
class TemplateInjectorParentComponent {
@ViewChild(TemplateRef) templateRef: TemplateRef<any>;
innerComponentValue = '';
}

@Directive({
selector: 'template-injector-inner',
})
class TemplateInjectorInnerDirective {
constructor(parent: TemplateInjectorParentComponent) {
parent.innerComponentValue = inject(TEMPLATE_INJECTOR_TEST_TOKEN);
}
}
13 changes: 8 additions & 5 deletions src/cdk/dialog/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ export class Dialog implements OnDestroy {
dialogRef: DialogRef<R, C>,
config: DialogConfig<D, DialogRef<R, C>>,
): BasePortalOutlet {
const userInjector = config.injector ?? config.viewContainerRef?.injector;
const userInjector = config.injector || config.viewContainerRef?.injector;
const providers: StaticProvider[] = [
{provide: DialogConfig, useValue: config},
{provide: DialogRef, useValue: dialogRef},
Expand Down Expand Up @@ -265,9 +265,8 @@ export class Dialog implements OnDestroy {
dialogContainer: BasePortalOutlet,
config: DialogConfig<D, DialogRef<R, C>>,
) {
const injector = this._createInjector(config, dialogRef, dialogContainer);

if (componentOrTemplateRef instanceof TemplateRef) {
const injector = this._createInjector(config, dialogRef, dialogContainer, undefined);
let context: any = {$implicit: config.data, dialogRef};

if (config.templateContext) {
Expand All @@ -283,6 +282,7 @@ export class Dialog implements OnDestroy {
new TemplatePortal<C>(componentOrTemplateRef, null!, context, injector),
);
} else {
const injector = this._createInjector(config, dialogRef, dialogContainer, this._injector);
const contentRef = dialogContainer.attachComponentPortal<C>(
new ComponentPortal(
componentOrTemplateRef,
Expand All @@ -301,14 +301,17 @@ export class Dialog implements OnDestroy {
* @param config Config object that is used to construct the dialog.
* @param dialogRef Reference to the dialog being opened.
* @param dialogContainer Component that is going to wrap the dialog content.
* @param fallbackInjector Injector to use as a fallback when a lookup fails in the custom
* dialog injector, if the user didn't provide a custom one.
* @returns The custom injector that can be used inside the dialog.
*/
private _createInjector<R, D, C>(
config: DialogConfig<D, DialogRef<R, C>>,
dialogRef: DialogRef<R, C>,
dialogContainer: BasePortalOutlet,
fallbackInjector: Injector | undefined,
): Injector {
const userInjector = config && config.viewContainerRef && config.viewContainerRef.injector;
const userInjector = config.injector || config.viewContainerRef?.injector;
const providers: StaticProvider[] = [
{provide: DIALOG_DATA, useValue: config.data},
{provide: DialogRef, useValue: dialogRef},
Expand All @@ -333,7 +336,7 @@ export class Dialog implements OnDestroy {
});
}

return Injector.create({parent: config.injector || userInjector || this._injector, providers});
return Injector.create({parent: userInjector || fallbackInjector, providers});
}

/**
Expand Down