-
Notifications
You must be signed in to change notification settings - Fork 6.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(dialog): add a config option for passing in data #2266
Conversation
@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'm the token master' }
]
}); 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: class SometDialog {
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
5f682e2
Compare
@shlomiassaf |
@crisbeto WRT typings, We could make export class MdDialogData<T> {
value: T;
} People could then inject, say, // In dialog-injector.ts
export const MD_DIALOG_DATA = new OpaqueToken('MdDialogData');
...
get(token: any, notFoundValue?: any): any {
if (token === MdDialogRef) {
return this._dialogRef;
} else if (token == MD_DIALOG_DATA) {
return config.data;
}
return this._parentInjector.get(token, notFoundValue);
} Then the user would inject the data: @Component({...})
export class UserProfileDialog(@Inject(MD_DIALOG_DATA) user: User) {
// ...
} The |
Yeah, something similar to the |
5f682e2
to
c819a32
Compare
@jelbourn Re-did this to use the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
src/lib/dialog/README.md
Outdated
| `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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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']); } } ``` Fixes angular#2181.
ba4119b
to
e366533
Compare
This should be good to go. The test failure is due to #2903 |
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! |
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
data
property. While this was previously possible through thedialogRef.componentInstance.someUserSuppliedProperty
, it wasn't the most intuitive. The new approach looks like this:Fixes #2181.