I am trying to use the Angular material module to open a model on click of a button. I have followed the example as suggested in the https://material.angular.io/components/dialog/examples.
However, I see that as try to run the application, I see the following error -
errors.js:48 ERROR Error: Uncaught (in promise): Error: StaticInjectorError[MatDialog]:
StaticInjectorError[MatDialog]:
NullInjectorError: No provider for MatDialog!
Error: StaticInjectorError[MatDialog]:
StaticInjectorError[MatDialog]:
NullInjectorError: No provider for MatDialog!
at _NullInjector.get (injector.js:31)
at resolveToken (injector.js:387)
at tryResolveToken (injector.js:330)
at StaticInjector.get (injector.js:170)
at resolveToken (injector.js:387)
at tryResolveToken (injector.js:330)
at StaticInjector.get (injector.js:170)
at resolveNgModuleDep (ng_module.js:103)
at NgModuleRef_.get (refs.js:1037)
at resolveDep (provider.js:455)
at _NullInjector.get (injector.js:31)
at resolveToken (injector.js:387)
at tryResolveToken (injector.js:330)
at StaticInjector.get (injector.js:170)
at resolveToken (injector.js:387)
at tryResolveToken (injector.js:330)
at StaticInjector.get (injector.js:170)
at resolveNgModuleDep (ng_module.js:103)
at NgModuleRef_.get (refs.js:1037)
at resolveDep (provider.js:455)
at resolvePromise (zone.js:824)
at resolvePromise (zone.js:795)
at eval (zone.js:873)
at ZoneDelegate.invokeTask (zone.js:425)
at Object.onInvokeTask (ng_zone.js:575)
at ZoneDelegate.invokeTask (zone.js:424)
at Zone.runTask (zone.js:192)
at drainMicroTaskQueue (zone.js:602)
at ZoneTask.invokeTask [as invoke] (zone.js:503)
at invokeTask (zone.js:1540)
defaultErrorLogger @ errors.js:48
I have the following setup of the project files
app.module.ts
I have imported the MatDialogModule and also placed the same in the imports. Within the Entry components, I have added the component that needs to be rendered in the modal.
import { MatDialogModule } from '@angular/material/dialog';
@NgModule({
imports: [ BrowserModule, BrowserAnimationsModule, FormsModule, NgbModule.forRoot(), ReactiveFormsModule,
HttpClientModule, routing, MatTabsModule, MatDialogModule ],
declarations: [ AppComponent, AppHeaderComponent, SignInComponent, RecruitmentHomeComponent, JobTemplatesComponent, CreateJobTemplateComponent ],
bootstrap: [ AppComponent ],
entryComponents: [CreateJobTemplateComponent],
providers: [AuthGuard, UserService, AuthenticationService]
})
Code in job-templates.component.ts
This is the file which is responsible for invoking the model on click of a button added to its template.
import { Component, OnInit } from '@angular/core';
import { CreateJobTemplateComponent } from './create-job-template/create-job-template.component';
import { MatDialog } from '@angular/material';
@Component({
selector: 'app-job-templates',
templateUrl: './job-templates.component.html',
styleUrls: ['./job-templates.component.css']
})
export class JobTemplatesComponent implements OnInit {
constructor(public dialog: MatDialog) { }
ngOnInit() {
}
createjobtemplate(){
let dialogRef = this.dialog.open(CreateJobTemplateComponent, {
width: '250px'
});
//Set the dialogRef property of opened dialog with the obtained ref
dialogRef.componentInstance.dialogRef = dialogRef;
}
}
create-job-template.component.ts
This component will render itself in the modal dialog.
import { Component, Inject, OnInit } from '@angular/core';
import { MatDialogRef } from '@angular/material';
import { FormGroup, FormBuilder } from '@angular/forms';
@Component({
selector: 'app-create-job-template',
templateUrl: './create-job-template.component.html',
styleUrls: ['./create-job-template.component.css']
})
export class CreateJobTemplateComponent implements OnInit {
form: FormGroup;
constructor(private formBuilder: FormBuilder, public dialogRef: MatDialogRef<CreateJobTemplateComponent>) { }
ngOnInit() {
this.form = this.formBuilder.group({
filename: ''
})
}
submit(form) {
this.dialogRef.close(`${form.value.filename}`);
}
}
Can anyone let me know where I am going wrong here? Most of the errors that I have looked for talk about failure to Inject MatDialogRef, but I find this error for MatDialog.
Also I was looking through this blog as well to make sure I followed the same steps. https://blog.thoughtram.io/angular/2017/11/13/easy-dialogs-with-angular-material.html
For Reference, I have the code at
https://stackblitz.com/edit/angular-fhhmff
Had the same error. Turns out I was importing
import { MatDialog } from '@angular/material';
Should have been
import { MatDialog } from '@angular/material/dialog';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With