Skip to content

Commit bf3596b

Browse files
committed
feat(material/dialog): add the ability to pass bindings
Adds the ability to pass a `Binding[]` to the component being rendered. Fixes #28791.
1 parent 867ba99 commit bf3596b

4 files changed

Lines changed: 21 additions & 2 deletions

File tree

goldens/material/dialog/index.api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
55
```ts
66

7+
import { Binding } from '@angular/core';
78
import { CdkDialogContainer } from '@angular/cdk/dialog';
89
import { ComponentPortal } from '@angular/cdk/portal';
910
import { ComponentRef } from '@angular/core';
@@ -125,6 +126,7 @@ export class MatDialogConfig<D = any> {
125126
ariaModal?: boolean;
126127
autoFocus?: AutoFocusTarget | string | boolean;
127128
backdropClass?: string | string[];
129+
bindings?: Binding[];
128130
closeOnNavigation?: boolean;
129131
closePredicate?: <Result = unknown, Component = unknown, Config extends DialogConfig = MatDialogConfig>(result: Result | undefined, config: Config, componentInstance: Component | null) => boolean;
130132
data?: D | null;

goldens/material/dialog/testing/index.api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import * as _angular_cdk_testing from '@angular/cdk/testing';
88
import { BaseHarnessFilters } from '@angular/cdk/testing';
9+
import { Binding } from '@angular/core';
910
import { CdkDialogContainer } from '@angular/cdk/dialog';
1011
import { ComponentHarnessConstructor } from '@angular/cdk/testing';
1112
import { ComponentPortal } from '@angular/cdk/portal';

src/material/dialog/dialog-config.ts

Lines changed: 6 additions & 2 deletions
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} from '@angular/core';
9+
import {ViewContainerRef, Injector, Binding} from '@angular/core';
1010
import {Direction} from '@angular/cdk/bidi';
1111
import {ScrollStrategy} from '@angular/cdk/overlay';
1212
import {DialogConfig, RestoreFocusValue} from '@angular/cdk/dialog';
@@ -159,5 +159,9 @@ export class MatDialogConfig<D = any> {
159159
*/
160160
exitAnimationDuration?: string | number;
161161

162-
// TODO(jelbourn): add configuration for lifecycle hooks, ARIA labelling.
162+
/**
163+
* Bindings to apply to the component rendered inside the dialog.
164+
* Does nothing for template-based dialogs.
165+
*/
166+
bindings?: Binding[];
163167
}

src/material/dialog/dialog.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
Directive,
2121
Injectable,
2222
Injector,
23+
Input,
2324
NgModule,
2425
TemplateRef,
2526
ViewChild,
@@ -28,6 +29,7 @@ import {
2829
forwardRef,
2930
signal,
3031
inject,
32+
inputBinding,
3133
} from '@angular/core';
3234
import {ComponentFixture, TestBed} from '@angular/core/testing';
3335
import {By} from '@angular/platform-browser';
@@ -736,6 +738,15 @@ describe('MatDialog', () => {
736738
expect(dialogRef.componentInstance.data).toBeNull();
737739
}).not.toThrow();
738740
});
741+
742+
it('should be able to apply bindings', () => {
743+
const dialogRef = dialog.open(PizzaMsg, {
744+
bindings: [inputBinding('flavor', () => 'pepperoni')],
745+
});
746+
viewContainerFixture.detectChanges();
747+
748+
expect(dialogRef.componentInstance!.flavor).toBe('pepperoni');
749+
});
739750
});
740751

741752
it('should not keep a reference to the component after the dialog is closed', async () => {
@@ -2340,6 +2351,7 @@ class ComponentWithTemplateRef {
23402351
changeDetection: ChangeDetectionStrategy.Eager,
23412352
})
23422353
class PizzaMsg {
2354+
@Input() flavor = 'unknown';
23432355
dialogRef = inject<MatDialogRef<PizzaMsg>>(MatDialogRef);
23442356
dialogInjector = inject(Injector);
23452357
directionality = inject(Directionality);

0 commit comments

Comments
 (0)