Skip to content

Commit 1a5d5d1

Browse files
committed
feat(cdk/dialog): add the ability to pass bindings
Adds the ability to pass a `Binding[]` to the component being rendered.
1 parent cb9148d commit 1a5d5d1

4 files changed

Lines changed: 27 additions & 2 deletions

File tree

goldens/cdk/dialog/index.api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ export class DialogConfig<D = unknown, R = unknown, C extends DialogContainer =
120120
ariaModal?: boolean;
121121
autoFocus?: AutoFocusTarget | string | boolean;
122122
backdropClass?: string | string[];
123+
bindings?: Binding[];
123124
closeOnDestroy?: boolean;
124125
closeOnNavigation?: boolean;
125126
closeOnOverlayDetachments?: boolean;

src/cdk/dialog/dialog-config.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import {ViewContainerRef, Injector, StaticProvider, Type} from '@angular/core';
9+
import {ViewContainerRef, Injector, StaticProvider, Type, Binding} from '@angular/core';
1010
import {Direction} from '../bidi';
1111
import {PositionStrategy, ScrollStrategy} from '../overlay';
1212
import {Observable} from 'rxjs';
@@ -191,4 +191,10 @@ export class DialogConfig<D = unknown, R = unknown, C extends DialogContainer =
191191
* A function can be passed in to resolve the context lazily.
192192
*/
193193
templateContext?: Record<string, any> | (() => Record<string, any>);
194+
195+
/**
196+
* Bindings to apply to the component rendered inside the dialog.
197+
* Does nothing for template-based dialogs.
198+
*/
199+
bindings?: Binding[];
194200
}

src/cdk/dialog/dialog.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ import {
1717
Directive,
1818
InjectionToken,
1919
Injector,
20+
Input,
2021
TemplateRef,
2122
ViewChild,
2223
ViewContainerRef,
2324
ViewEncapsulation,
2425
inject,
26+
inputBinding,
2527
} from '@angular/core';
2628
import {ComponentFixture, TestBed} from '@angular/core/testing';
2729
import {By} from '@angular/platform-browser';
@@ -606,6 +608,15 @@ describe('Dialog', () => {
606608
expect(dialogRef.componentInstance!.data).toBeNull();
607609
}).not.toThrow();
608610
});
611+
612+
it('should be able to apply bindings', () => {
613+
const dialogRef = dialog.open(PizzaMsg, {
614+
bindings: [inputBinding('flavor', () => 'pepperoni')],
615+
});
616+
viewContainerFixture.detectChanges();
617+
618+
expect(dialogRef.componentInstance!.flavor).toBe('pepperoni');
619+
});
609620
});
610621

611622
it('should not keep a reference to the component after the dialog is closed', () => {
@@ -1364,6 +1375,7 @@ class ComponentWithTemplateRef {
13641375
changeDetection: ChangeDetectionStrategy.Eager,
13651376
})
13661377
class PizzaMsg {
1378+
@Input() flavor = 'unknown';
13671379
dialogRef = inject<DialogRef<PizzaMsg>>(DialogRef);
13681380
dialogInjector = inject(Injector);
13691381
directionality = inject(Directionality);

src/cdk/dialog/dialog.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,13 @@ export class Dialog implements OnDestroy {
302302
} else {
303303
const injector = this._createInjector(config, dialogRef, dialogContainer, this._injector);
304304
const contentRef = dialogContainer.attachComponentPortal<C>(
305-
new ComponentPortal(componentOrTemplateRef, config.viewContainerRef, injector),
305+
new ComponentPortal(
306+
componentOrTemplateRef,
307+
config.viewContainerRef,
308+
injector,
309+
null,
310+
config.bindings,
311+
),
306312
);
307313
(dialogRef as {componentRef: ComponentRef<C>}).componentRef = contentRef;
308314
(dialogRef as {componentInstance: C}).componentInstance = contentRef.instance;

0 commit comments

Comments
 (0)