The close() method for Angular Material Dialog's dialogref is malfunctioning

Why isn't the close function working? It seems like dialogRef.close() is undefined.

Below is the code snippet:

   <button  mat-raised-button (click)="openModal()">Open Project Specifics</button>

TS

  openModal(){
   let dialogRef = this.dialog.open(ProjectSpecificContentComponent, {
   data:{projectsSpecifics: this.projectSpecific},
      panelClass: 'project-content-dialog'
 })
     dialogRef.afterClosed().subscribe(result => console.log(result))
  }

This is the Component:

    <button mat-dialog-close>X</button>

              <div class="container">
                <div class="project-specific" *ngFor="let projectS of projectSpecificList">
               <h5>{{projectS.name}}</h5>
                <mat-form-field appearance="fill" class="mat-group">
                 <mat-label>Add project specific</mat-label>
          <mat-select multiple>
            <mat-option *ngFor="let item of getContent(projectS)">{{item.content}}</mat- 
            option>
          </mat-select>
        </mat-form-field>
         </div>

       </div>
       <div mat-dialog-actions>
         <button (click)="onClose()" mat-raised-button>Done!</button>
       </div>

TS

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



                      onClose(){
                        this.dialogRef.close();
                        }

This is where the component is imported:

   imports[MatDialogModule]
   entryComponents: [ProjectSpecificContentComponent]

Answer №1

Here is an example of my TypeScript dialog code:

export class DialogComponent implements OnInit {
  constructor(
    public dialogReference: MatDialogRef<DialogComponent>, 
  )

  closeDialog(){
    this.dialogReference.close();
  }
}

Feel free to give it a try!

Answer №2

First Step:

make sure to include public dialogRef: MatDialogRef<any> in your constructor

Next, assign your local dialogRef variable to the global this.dialogRef variable

this.dialogRef = dialogRef;

Lastly, call this.dialogRef.close() to execute the closing of the dialog

Hopefully, this solution will be effective for you.

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

What is the best way to verify a numerical input in a React component?

When I use the return statement, I attempt to validate a number and if it's not valid, assign a value of 0. However, this approach doesn't seem to be working for me. Is there an alternative method to achieve this? return ( <Input col ...

How can I designate the first enum value as 1 in a protobuf data structure?

In order to resolve an issue in the dropdown component of Primeng, the first enum value in protobuf must be set to 0. However, this is causing some trouble. Is there a method to change the first enum value to 1 instead? ...

When navigating using the next and back buttons, the active state in Angular is automatically removed

Looking for some assistance with my quiz app setup. Each question has True/False statements with corresponding buttons to select T or F. However, when I click the next/back button, the active class is not being removed from the previous selection. As a beg ...

The Datatable is displaying the message "The table is currently empty" despite the fact that the rows have been loaded through Angular

My experience with displaying a data table in Angular was frustrating. Even though all the records were present in the table, it kept showing "no data available." Additionally, the search function refused to work as intended. Whenever I tried searching for ...

How can I retrieve a value from a jQuery function within an external Angular function?

implement{ let selection; $(".l_item").click(function(){ selection = this.value; }); console.log(this.selection); } This unique jQuery function retrieves the selected value from the l_item array of lists. After obtaining the value, there ...

What could be causing my D3.js stacked bar chart to show inaccurate results?

I'm encountering some challenges while creating a stacked bar chart in d3.js. The current appearance of my chart is depicted here (developed with D3.js): https://i.sstatic.net/G6UA6.png However, I aim to achieve a design similar to this one (crafted ...

Here's a new take on the topic: "Implementing image change functionality for a specific div in Angular 8 using data from a loop"

I need to create a list with dynamic data using a loop. When I click on any item in the list, I want the image associated with that item to change to a second image (dummyimage.com/300.png/09f/fff) to indicate it's active. This change should persist e ...

Challenges arise when dealing with generics in TypeScript

I'm a beginner in TypeScript and I'm trying to write a method with a generic type argument similar to what you can do in .Net. Here's the code snippet I've been working on: class TestObject { Id: number; Truc: string; Machin: str ...

Showing dynamic icons in Angular 2 applications

My goal is to dynamically load a part of my website, specifically by using icon classes defined in the interface like this: import { OpaqueToken } from "@angular/core"; import {IAppConfig} from './app.interface' export let APP_CONFIG = new Opaq ...

Looking for a way to detect changes in a select menu using Angular?

How can I determine with the openedChange event if there have been any changes to the select box items when the mat select panel is closed or opened? Currently, I am only able to detect if the panel is open or closed. I would like to be able to detect any ...

Creating a Union Type from a JavaScript Map in Typescript

I am struggling to create a union type based on the keys of a Map. Below is a simple example illustrating what I am attempting to achieve: const myMap = new Map ([ ['one', <IconOne/>], ['two', <IconTwo/>], ['three ...

The argument provided needs to be a function, but instead, an object instance was received, not the original argument as expected

I originally had the following code: const util = require('util'); const exec = util.promisify(require('child_process').exec); But then I tried to refactor it like this: import * as exec from 'child_process'; const execPromis ...

Is there a way to retrieve the Angular-Redux store in a child module?

Within my Angular application, I utilize angular-redux for managing the application state. In my main module, I have defined the redux store in the following manner: export class MainModule { constructor(private ngRedux: NgRedux<MainAppState>, ...

Can the PrimeNG p-fileUpload component be configured to launch from a specific directory?

Utilizing the PrimeNG p-fileUpload component for file uploads. Looking to customize the default folder that opens when the select file button is clicked. Would like it to open in a specific location such as Desktop or Videos. Is there a method to achieve ...

Fulfill the promise to retrieve the value contained within

Is there a way to use TypeScript to call the Wikipedia API for retrieving a random page title and save it in a variable for later use? I am struggling with resolving promises as I keep getting ZoneAwarePromise returned. I'm new to both promises and Ty ...

Developing applications using ReactJS with Typescript can sometimes lead to errors, such as the "onclick does not exist on type x

In the code snippet below, I have a method that renders a delete icon and is used in my main container. Everything functions correctly except for a small cosmetic issue related to the type any that I am struggling to identify. import React from 'reac ...

Using NextJS: Issue with updating Value in useState

In my current project, I am attempting to display a string that changes when a button is pressed in my NextJs application. Here's the code snippet I am working with: 'use client' import { useState } from 'react' export default fu ...

Is it necessary to unsubscribe from an Angular HTTP-Request when using switchMap?

I have a question about managing subscriptions in my Angular application. I'm currently using the combineLatest operator to create an observable from two Angular observables, route.params and route.queryParams. This combined observable is then used as ...

Tips for making unique Angular Material mat-menu-items from scratch

Is there a way to fully customize how menu items appear, including displaying multiple lines of text with different font sizes and colors? Currently, it seems that only the first line of text is being shown. For example: <mat-menu> <div mat-menu ...

ngClass is failing to be implemented

Does anyone know how to dynamically apply a CSS style to an input based on a variable value? HTML <input class="defaultInput" [ngClass]="{'inputerror':'emptyFields'}" formControlName="idAnnuaire" placeholder="Ex: c20011"> CSS ...