Skip to content
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

ERROR Error: No provider for Token MdDialogData! #4086

Closed
kelsmj opened this issue Apr 13, 2017 · 6 comments · Fixed by #4120
Closed

ERROR Error: No provider for Token MdDialogData! #4086

kelsmj opened this issue Apr 13, 2017 · 6 comments · Fixed by #4120
Assignees
Labels
P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent

Comments

@kelsmj
Copy link

kelsmj commented Apr 13, 2017

Bug, feature request, or proposal:

As of 2.0.0-beta.3, you need to inject the MD_DIALOG_DATA token in order to access the data property of the MdDialogConfig object. If you inject the MD_DIALOG_DATA token into a component that will be loaded into the dialog, and you do not pass data in, you will get

Error: No provider for Token MdDialogData!

What is the expected behavior?

Do not throw an error and to return an empty object if none is passed in.

What is the current behavior?

Error: No provider for Token MdDialogData!

What are the steps to reproduce?

Plunker

What is the use-case or motivation for changing an existing behavior?

Which versions of Angular, Material, OS, browsers are affected?

Angular Material 2.0.0-beta.3
Angular 4.00

Is there anything else we should know?

@jelbourn jelbourn added the P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent label Apr 13, 2017
@crisbeto
Copy link
Member

crisbeto commented Apr 17, 2017

Throwing an error, if no data is supplied, was intentional, but I can see how it could hinder re-usability. I'll add a default value for it. Until it gets in you can work around it by making the data optional (e.g. @Optional() @Inject(MD_DIALOG_DATA) data: any).

crisbeto added a commit to crisbeto/material2 that referenced this issue Apr 17, 2017
Adds a default value for the `MD_DIALOG_DATA` injection token, instead of throwing an error when it's not defined.

Fixes angular#4086.
@dkontorovskyy
Copy link

@optional() @Inject(MD_DIALOG_DATA) data: any

This solution is not working either.

Getting

Error: Uncaught (in promise): Error: No provider for t!

@fvoska
Copy link

fvoska commented Jan 5, 2018

Small note for testing components which inject MAT_DIALOG_DATA:

constructor(
  public dialogRef: MatDialogRef<PopupDialogComponent>,
  @Inject(MAT_DIALOG_DATA) protected data: DialogData
)

You can add this to your TestBed configuration:

providers: [{
  provide: MatDialogRef,
  useValue: {}
}, {
  provide: MAT_DIALOG_DATA,
  useValue: {} // Add any data you wish to test if it is passed/used correctly
}]

@gundupatil
Copy link

data which you are passing to dialog might be misssing

@kobusbeets
Copy link

kobusbeets commented Oct 9, 2018

Just for future reference.

This worked for me when I faced the same issue today. Add @Optional() to both injections e.g.

constructor(
    @Optional() public dialogRef: MatDialogRef<PopupComponent>,
    @Optional() @Inject(MAT_DIALOG_DATA) public data: any
) {}

They will have a null value if the component is not used as a popup. If I omit the @Optional() declaration from any of those two injections I get the same error.

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 9, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants