Skip to content

Commit ff8d56e

Browse files
authored
refactor(dialog): remove custom injector usage (#18565)
Replaces the usage of our custom `PortalInjector` with Angular's `Injector`. The advantage of using the default one is that the injection logic is consistent with everything else and we don't have to require users to polyfill `WeakMap`. The idea here is to eventually replace all usages of `PortalInjector` with `Injector` and to deprecate it, but I wanted to try it out against `dialog` since it's probably the most used and well tested of the components that are using the custom injector.
1 parent 1c2300f commit ff8d56e

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

src/material/dialog/dialog.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
OverlayRef,
1515
ScrollStrategy,
1616
} from '@angular/cdk/overlay';
17-
import {ComponentPortal, ComponentType, PortalInjector, TemplatePortal} from '@angular/cdk/portal';
17+
import {ComponentPortal, ComponentType, TemplatePortal} from '@angular/cdk/portal';
1818
import {Location} from '@angular/common';
1919
import {
2020
Inject,
@@ -25,6 +25,7 @@ import {
2525
Optional,
2626
SkipSelf,
2727
TemplateRef,
28+
StaticProvider,
2829
} from '@angular/core';
2930
import {defer, Observable, of as observableOf, Subject} from 'rxjs';
3031
import {startWith} from 'rxjs/operators';
@@ -215,9 +216,11 @@ export class MatDialog implements OnDestroy {
215216
*/
216217
private _attachDialogContainer(overlay: OverlayRef, config: MatDialogConfig): MatDialogContainer {
217218
const userInjector = config && config.viewContainerRef && config.viewContainerRef.injector;
218-
const injector = new PortalInjector(userInjector || this._injector, new WeakMap([
219-
[MatDialogConfig, config]
220-
]));
219+
const injector = Injector.create({
220+
parent: userInjector || this._injector,
221+
providers: [{provide: MatDialogConfig, useValue: config}]
222+
});
223+
221224
const containerPortal = new ComponentPortal(MatDialogContainer,
222225
config.viewContainerRef, injector, config.componentFactoryResolver);
223226
const containerRef = overlay.attach<MatDialogContainer>(containerPortal);
@@ -283,29 +286,29 @@ export class MatDialog implements OnDestroy {
283286
private _createInjector<T>(
284287
config: MatDialogConfig,
285288
dialogRef: MatDialogRef<T>,
286-
dialogContainer: MatDialogContainer): PortalInjector {
289+
dialogContainer: MatDialogContainer): Injector {
287290

288291
const userInjector = config && config.viewContainerRef && config.viewContainerRef.injector;
289292

290293
// The MatDialogContainer is injected in the portal as the MatDialogContainer and the dialog's
291294
// content are created out of the same ViewContainerRef and as such, are siblings for injector
292295
// purposes. To allow the hierarchy that is expected, the MatDialogContainer is explicitly
293296
// added to the injection tokens.
294-
const injectionTokens = new WeakMap<any, any>([
295-
[MatDialogContainer, dialogContainer],
296-
[MAT_DIALOG_DATA, config.data],
297-
[MatDialogRef, dialogRef]
298-
]);
297+
const providers: StaticProvider[] = [
298+
{provide: MatDialogContainer, useValue: dialogContainer},
299+
{provide: MAT_DIALOG_DATA, useValue: config.data},
300+
{provide: MatDialogRef, useValue: dialogRef}
301+
];
299302

300303
if (config.direction &&
301304
(!userInjector || !userInjector.get<Directionality | null>(Directionality, null))) {
302-
injectionTokens.set(Directionality, {
303-
value: config.direction,
304-
change: observableOf()
305+
providers.push({
306+
provide: Directionality,
307+
useValue: {value: config.direction, change: observableOf()}
305308
});
306309
}
307310

308-
return new PortalInjector(userInjector || this._injector, injectionTokens);
311+
return Injector.create({parent: userInjector || this._injector, providers});
309312
}
310313

311314
/**

0 commit comments

Comments
 (0)