Skip to content

Commit 4e165f9

Browse files
committed
fix(cdk/dialog): use config injector if provided
1 parent fd2811b commit 4e165f9

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/cdk/dialog/dialog.spec.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
Directive,
1313
Inject,
1414
Injector,
15+
Optional,
1516
TemplateRef,
1617
ViewChild,
1718
ViewContainerRef,
@@ -125,6 +126,21 @@ describe('Dialog', () => {
125126
);
126127
});
127128

129+
it('should use custom test injector', () => {
130+
// Create a custom injector for the component.
131+
let viewContainerFixtureChocolateInjector = TestBed.createComponent(PizzaSnack);
132+
viewContainerFixtureChocolateInjector.detectChanges();
133+
let chocolateInjector = new ChocolateInjector(
134+
viewContainerFixtureChocolateInjector.componentInstance.injector,
135+
);
136+
137+
let dialogRef = dialog.open(PizzaSnack, {
138+
injector: chocolateInjector,
139+
});
140+
141+
expect(dialogRef.componentInstance!.snack.toString()).toBe('Chocolate');
142+
});
143+
128144
it('should open a dialog with a component and no ViewContainerRef', () => {
129145
let dialogRef = dialog.open(PizzaMsg);
130146

@@ -1223,3 +1239,26 @@ class DialogWithoutFocusableElements {}
12231239
encapsulation: ViewEncapsulation.ShadowDom,
12241240
})
12251241
class ShadowDomComponent {}
1242+
1243+
class Chocolate {
1244+
toString() {
1245+
return 'Chocolate';
1246+
}
1247+
}
1248+
1249+
class ChocolateInjector {
1250+
constructor(public parentInjector: Injector) {}
1251+
1252+
get(token: any) {
1253+
return token === Chocolate ? new Chocolate() : this.parentInjector.get<any>(token);
1254+
}
1255+
}
1256+
1257+
/** Simple component for testing custom injector. */
1258+
@Component({
1259+
selector: 'pizza-snack',
1260+
template: '<p>Pizza</p><p>{{snack}}</p><ng-content></ng-content>',
1261+
})
1262+
class PizzaSnack {
1263+
constructor(@Optional() public snack: Chocolate, public injector: Injector) {}
1264+
}

src/cdk/dialog/dialog.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ export class Dialog implements OnDestroy {
333333
});
334334
}
335335

336-
return Injector.create({parent: userInjector || this._injector, providers});
336+
return Injector.create({parent: config.injector || userInjector || this._injector, providers});
337337
}
338338

339339
/**

0 commit comments

Comments
 (0)