The functionality to close the Angular Material Dialog is not functioning as expected

For some reason, I am facing an issue with closing a dialog window in Angular Material using mat-dialog-close.

I have ensured that my NgModule has BrowserAnimationModule and MatDialogModule included after checking online resources.

Your assistance with the provided code would be greatly appreciated.

ModalDialog.html

<div mat-dialog-actions>
  <button mat-button mat-dialog-close>Cancel</button>
</div>
<div class="modal-container">
  <h1 mat-dialog-title>{{ data }}</h1>
</div>

Register.component.ts

import { Component, Inject, OnInit } from '@angular/core';
import { FormControl, Validators } from '@angular/forms';
import { MatDialog, MAT_DIALOG_DATA } from '@angular/material/dialog';
import { states } from '../constants/states';

@Component({
  selector: 'app-register',
  templateUrl: './register.component.html',
  styleUrls: ['./register.component.css'],
})
export class RegisterComponent implements OnInit {
  constructor(public dialog: MatDialog) {}

  // Function to open the modal for consent form
  openModal() {
    this.dialog.open(ModalDialog, {
      data: `Some random Data`    
   });
  }

  ngOnInit(): void {}
}

// Modal code from angular material
@Component({
  selector: 'modal-dialog',
  templateUrl: 'ModalDialog.html',
  styleUrls: ['./register.component.css'],
})
export class ModalDialog {
  constructor(@Inject(MAT_DIALOG_DATA) public data: string) {}
}

register.html

<div class="row-five">
  <mat-checkbox></mat-checkbox>
  <p>
    I have read this
    <span
      (click)="openModal()"
      style="color: blue; text-decoration: underline; cursor: pointer"
    >consent form</span
    >
    and I fully understand the contents of this document and voluntarily
    consent to participate in this camp*
  </p>
</div>

Edit Versioning of libraries.

Package.json

"dependencies": {
    "@angular/animations": "~13.3.0",
    "@angular/cdk": "^13.3.7",
    "@angular/common": "~13.3.0",
    "@angular/compiler": "~13.3.0",
    "@angular/core": "~13.3.0",
    "@angular/forms": "~13.3.0",
    "@angular/material": "^13.3.7",
    "@angular/platform-browser": "~13.3.0",
    "@angular/platform-browser-dynamic": "~13.3.0",
    "@angular/router": "~13.3.0",
    "rxjs": "~7.5.0",
    "tslib": "^2.3.0",
    "zone.js": "~0.11.4"
  },

Edit Attempting to use it as an element caused this error.

<mat-dialog-actions>
  <button mat-button mat-dialog-close>Cancel</button>
</mat-dialog-actions>
<div class="modal-container">
  <h1 mat-dialog-title>{{ data }}</h1>
</div>

I encountered the following error message:

'mat-dialog-actions' is not a known element:
1. If 'mat-dialog-actions' is an Angular component, then verify that it is part of this module.
2. If 'mat-dialog-actions' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.ngtsc(-998001)

Answer №1

According to information found in the resources, it is specified that mat-dialog-actions should be treated as an element rather than an attribute.

Have you experimented with implementing it in this manner?

<mat-dialog-actions>
  <button mat-button mat-dialog-close>Cancel</button>
</mat-dialog-actions>
<div class="modal-container">
  <h1 mat-dialog-title>{{ data }}</h1>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Challenges Experienced by AoT in Live Operations

So in my project, I have various components and services included where necessary. To ensure their accessibility, I made sure to declare all the services as private within the constructor. Here's an example: constructor(private myService: MyService) ...

Encountering issues with Proxy functionality in the latest versions of Angular 13 and Spring Boot

I've encountered an issue with the proxy configuration in Angular. I'm unsure if it's a problem within my Angular settings or if there's a configuration issue in Spring. For testing purposes, I have a backend built in springboot to han ...

The Angular variable binding issue persists upon reloading the page or browser, yet functions seamlessly when navigating between routes

My subscribe button displays the text "Subscribe" when the page loads, but upon reloading the page, the text disappears. The button text is controlled by TypeScript code, and strangely, when I navigate to another route, the text magically reappears. HTML ...

Using TypeScript: Union Types for Enum Key Values

Here's the code in the TS playground too, click here. Get the Enum key values as union types (for function parameter) I have managed to achieve this with the animals object by using key in to extract the key as the enum ANIMALS value. However, I am s ...

I am experiencing an issue with the PUT method on my API as it is not correctly setting the req.body data

Below is the code snippet for implementing the PUT method: [/api/[id].ts] case "PUT": try { const user = await UserModel.findOneAndUpdate( { _id: id, }, { $set: req.body, ...

Angular: Defining variables using let and var

When working with TypeScript and JavaScript, we typically use either let or var to declare a variable. However, in Angular components, we do not use them even though Angular itself uses TypeScript. For instance, export class ProductComponent implements OnI ...

Tips for creating a responsive swiper slider in an Angular project

In my Angular project, I am using a swiper slider with 4 items in desktop view. However, I would like to display only 1 item in the mobile view. You can see the code at this link: https://stackblitz.com/edit/ngx-swiper-wrapper-demo-h9egdh?file=app/app.com ...

When trying to use the exported Ionic 3 class in TypeScript, the error "Cannot find name 'ClassName'" occurs

When working on my Ionic 3 Application, I encountered an error stating "Cannot find name" when trying to use certain plugins. I initially imported the plugin like this: import { AndroidPermissions } from '@ionic-native/android-permissions'; and ...

Is there a way to retrieve the resolved data within the route definition in Angular?

I am looking to execute some actions within the route definition once the lazyloaded module is loaded. The route includes a resolver called UserDataResolverService. How can I retrieve and utilize the resolved data within the route definition? { ...

React slick slider not functioning properly with custom arrows

"I have encountered an issue while trying to implement multiple sliders in my component with custom arrows positioned below each carousel. Despite following the documentation meticulously, the arrows do not respond when clicked. What could possibly be ...

What is the best way to retry an action stream observable in Angular/RxJS after it fails?

Kindly disregard the variable names and formatting alterations I've made. I've been attempting to incorporate RxJS error handling for an observable that triggers an action (user click) and then sends the request object from our form to execute a ...

Unable to locate the control with the name "email" or "pwd", and the onSubmit function is not defined

I am currently facing an issue with form validation using reactive forms. The problem seems to be related to the console errors indicating that it cannot find controls named 'email' and 'pwd'. This is a basic sign-in form, and here is t ...

Developing bespoke styles in Angular Material 2

I am in the process of developing a unique theme for my Angular 2 application, incorporating various components from angular material 2. Despite searching extensively online, I haven't been able to find much relevant information. The only documentati ...

Why hasn't the variable been defined?

Why am I receiving an error message saying "test is not defined" in this code? Even though I have properly defined the variable in another service file, it seems to be causing issues here. Any insights on what could be going wrong? import { Injectable } f ...

Are there any solutions to refresh a page by clicking a button in next.js?

I am currently learning how to work with next.js and I'm facing an issue where I need to reload my page to make a new API request. As a beginner, I'm not sure how to achieve this. I've tried a few methods but none of them seem to work. Below ...

Mapped TypeScript type requiring scalar properties and allowing optional objects

I am in need of a TypeScript generic type that has the capability to alter another type so that all scalar properties (such as strings, numbers, booleans, etc.) remain mandatory, while object types become optional. For instance, given the User type below, ...

Angular 2's Http Post function mistakenly triggers dual ajax requests instead of a single one

Greetings! I have been delving into the implementation of the observable pattern in Angular and seem to be facing some issues. Below is the code snippet that is causing trouble: import 'rxjs/Rx'; import {Injectable, EventEmitter} from 'ang ...

Exploring the functionality of generic components in React Native when using TypeScript

As an illustration, consider export class FlatList<ItemT> extends React.Component<FlatListProps<ItemT>> which incorporates the generic type ItemT. How can I utilize it in a .tsx code? When not parametrized, it appears like this: <Flat ...

Contrasting importing a module in app.module versus a component

Angular 5 Can you explain the distinction between importing a module in app.module versus importing it directly into the component where it is needed? In particular, why is it necessary for a module to be included in node modules, app.module, the import ...

What is the reason behind TypeScript rejecting the syntax of checkbox input elements?

When trying to use the following checkbox in TypeScript, I encountered a warning: <input type="checkbox" onClick={(event: React.MouseEvent<HTMLInputElement>) => { setIsTOSAccepted(event.target.checked); }} defaultChecked={ ...