Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<mat-dialog-title> and <mat-dialog> are not know recognized but <mat-dialog-actions> and <mat-dialog-content> is recognized

Browser used - Chrome 67.0.3396.99


I have created a DialogsModule which has a component ConfirmDialog.component.ts with the following template confirm-dialog.component.html

confirm-dialog.component.html

    <mat-dialog>    
        <mat-dialog-title>{{title}}</mat-dialog-title>
        <mat-dialog-content>{{message}}</mat-dialog-content>
        <mat-dialog-actions> 
            <button type="button" mat-raised-button color="primary"
                (click)="dialogRef.close(true)">Yes</button>
            <button type="button" mat-button 
                (click)="dialogRef.close()">No</button>
         </mat-dialog-actions>
    </mat-dialog>

I am getting the following error

    *Uncaught Error: Template parse errors:
    'mat-dialog-title' is not a known element:
    1. If 'mat-dialog-title' is an Angular component, then verify that it is part of this module.
    2. If 'mat-dialog-title' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("<mat-dialog> [ERROR ->]<mat-dialog-title>{{title}}</mat-dialog-title>
    <mat-dialog-content>{{message}}</mat-dialog-content>"): ng:///DialogsModule/ConfirmDialog.html@2:1
    'mat-dialog' is not a known element:
    1. If 'mat-dialog' is an Angular component, then verify that it is part of this module.
    2. If 'mat-dialog' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("[ERROR ->]<mat-dialog> 
    <mat-dialog-title>{{title}}</mat-dialog-title>
    <mat-dialog-content>{{message}}</mat"): ng:///DialogsModule/ConfirmDialog.html@1:0*

If I remove <mat-dialog> and <mat-dialog-title> element from the template then everything works fine.


Below posted is my source code.

confirm-dialogs.component.ts

    import { MatDialogRef } from '@angular/material';
    import { Component } from '@angular/core';

    @Component({
        selector: 'confirm-dialog',
        templateUrl: './confirm-dialog.component.html',
        styleUrls: ['./dialogs.scss']
    })
    export class ConfirmDialog {

        public title: string;
        public message: string;

        constructor(public dialogRef: MatDialogRef<ConfirmDialog>) {
        }
    }

Dialogs.module.ts

    import { DialogsService } from './dialogs.service';
    import { MatDialogModule, MatButtonModule } from '@angular/material';
    import { NgModule } from '@angular/core';

    import { ConfirmDialog }   from './confirm-dialog.component';

    @NgModule({
        imports: [
            MatDialogModule,
            MatButtonModule,
        ],
        exports: [
            ConfirmDialog,
        ],
        declarations: [
            ConfirmDialog,
        ],
        providers: [
            DialogsService,
        ],
        entryComponents: [
            ConfirmDialog,
        ],
    })
    export class DialogsModule { }

dialogs.service.ts

    import { Observable } from 'rxjs';
    import { ConfirmDialog } from './confirm-dialog.component';
    import { MatDialogRef, MatDialog, MatDialogConfig } from '@angular/material';
    import { Injectable } from '@angular/core';

    @Injectable({
        providedIn: 'root'
    })
    export class DialogsService {

        constructor(private dialog: MatDialog) { }

        public confirm(title: string, message: string): Observable<boolean> {

            let dialogRef: MatDialogRef<ConfirmDialog>;

            dialogRef = this.dialog.open(ConfirmDialog);
            dialogRef.componentInstance.title = title;
            dialogRef.componentInstance.message = message;

            return dialogRef.afterClosed();
        }
    }

And finally the calling code

    let res1
        this.dialogsService
          .confirm('Confirm Dialog', 'Are you sure you want to do this?')
          .subscribe(res => console.log(res));```
like image 414
Deep Nishad Avatar asked Jul 12 '18 05:07

Deep Nishad


1 Answers

mat-dialog-title should be rather used as a directive and not a component

<title mat-dialog-title>{{title}}<//title>

And also I don't think there is mat-dialog directive or component from angular/material.

like image 50
Amit Chigadani Avatar answered Oct 20 '22 13:10

Amit Chigadani