Uh oh!
There was an error while loading. Please reload this page.
feat(dialog): add a config option for passing in data - #2266
Conversation
crisbeto
commented
Dec 18, 2016
@jelbourn I'm not huge fan of having to access the properties on the |
An option could be to have the |
@crisbeto@jelbourn This is not what I suggested in #2181 Let the user pass providers and you add them into the injector. When you instantiate the component the user has provided you already add something to the injector the This is what I had in mind: dialog.open(SomeDialog,{providers : [{provider: MyPreciousToken,useValue: 'I'mthetokenmaster' }]});You can of course use The current PR doesn't change much, you still don't get natural type support, if you want type support you need to do: classSometDialog{constructor(@Inject(MdDialogData)data: MyHelloDataModel){console.log(data.hello);}}If you insist on going that road I suggest changing But again, i'm pretty sure once more people use it that you will get a request for provider injection (or injector replacement) since the user can't control it. I have a modal library for angular from alpha 17, a lot of people use the provider injection. |
9591ab0 to
5f682e2Comparejelbourn
commented
Jan 13, 2017
@shlomiassaf |
jelbourn
commented
Jan 13, 2017
@crisbeto WRT typings, We could make exportclassMdDialogData<T>{value: T;}People could then inject, say, // In dialog-injector.tsexportconstMD_DIALOG_DATA=newOpaqueToken('MdDialogData');
...
get(token: any,notFoundValue?: any): any {if(token===MdDialogRef){returnthis._dialogRef;}elseif(token==MD_DIALOG_DATA){returnconfig.data;}returnthis._parentInjector.get(token,notFoundValue);}Then the user would inject the data: @Component({...})exportclassUserProfileDialog(@Inject(MD_DIALOG_DATA)user: User){// ...}The |
Yeah, something similar to the |
5f682e2 to
c819a32Comparecrisbeto
commented
Jan 21, 2017
@jelbourn Re-did this to use the |
| | `viewContainerRef: ViewContainerRef` | The view container ref to attach the dialog to. Optional. | | ||
| | `role?: DialogRole = 'dialog'` | The ARIA role of the dialog element. Possible values are `dialog` and `alertdialog`. | | ||
| | `disableClose?: boolean = false` | Whether to prevent the user from closing a dialog by clicking on the backdrop or pressing escape. | | ||
| | `data?: { [key: string]: any }` | Data that will be injected into the dialog as via the `MD_DIALOG_DATA` token. | |
There was a problem hiding this comment.
The type of data should be any
Adds the ability to pass in data to a dialog via the `data` property. While this was previously possible through the `dialogRef.componentInstance.someUserSuppliedProperty`, it wasn't the most intuitive. The new approach looks like this:
```
dialog.open(SomeDialog, {
data: {
hello: 'world'
}
});
class SometDialog {
constructor(data: MdDialogData) {
console.log(data['hello']);
}
}
```
Fixesangular#2181.ba4119b to
e366533Comparecrisbeto
commented
Feb 4, 2017
This should be good to go. The test failure is due to #2903 |
sum0mer
commented
Mar 1, 2017
Hi, I tried to 'import {MdDialog, MdDialogRef, MdDialogData} from '@angular/material';', but got [workspace/angular2/node_modules/@angular/material/index"' has no exported member 'MdDialogData'] compile error. Any suggestion to make it work? Thanks! |
crisbeto
commented
Mar 1, 2017
We didn't end up using |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Adds the ability to pass in data to a dialog via the
dataproperty. While this was previously possible through thedialogRef.componentInstance.someUserSuppliedProperty, it wasn't the most intuitive. The new approach looks like this:Fixes#2181.