For mdDialog, how do I pass in variable? Specifically, how to inject an Angular service into the dialog component?
相关问题
- Angular RxJS mergeMap types
- npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fs
- How to update placeholder text in ng2-smart-table?
- How to instantiate Http service in main.ts manuall
- Angular: ngc or tsc?
相关文章
- angular脚手架在ie9+下兼容问题
- angular 前端项目 build 报错 "Cannot find module 'le
- Angular Material Stepper causes mat-formfield to v
- After upgrade to Angular 9 cannot find variable in
- is there any difference between import { Observabl
- Suppress “Circular dependency detected” suppress w
- How can you get current positional information abo
- Angular material table not showing data
From the official docs found on https://material.angular.io/components/dialog/overview
Sharing data with the Dialog component.
If you want to share data with your dialog, you can use the data option to pass information to the dialog component.
To access the data in your dialog component, you have to use the MD_DIALOG_DATA injection token:
To give an updated answer to accommodate for the update from 'Md' to 'Mat':
To open the dialog with data, pass in a data object:
To retrieve that data in your dialog:
Material2 beta.2
The
dialog.open()
function takes a 2nd parameterconfig
(MdDialogConfig) where you can specify anydata
object.You can then just retrieve this object from the component that is being used for your dialog window.
UPDATE: beta.3
The answer above works for the version
2.0.0-beta.2
of Material2. If you are using2.0.0-beta.3
, theconfig
property was removed fromMdDialogRef
. you can instead inject that value using theMD_DIALOG_DATA
of the opened component.New import statements
OPEN DIALOG
RETRIEVE DATA FROM
DialogRef
componentFor passing variables you can grab the instance of the component opened in the dialog, from the MdDialogRef instance returned in the MdDialog.open() method call.
Modified Pizza from the github material2 docs
Here's how I did it.
pizza.service.ts
pizzaDialog.component.ts