The issue arises when using Angular Material as it seems that passing a data object to a matdialog dialog

After reviewing multiple posts and carefully examining the process of passing data from an Angular Component to MatDialog, I am facing an issue where 'undefined' is being returned when the dialog loads.

Below is the code snippet I have been working on:

import { MatDialog } from '@angular/material';

let dialogRef = this.dialog.open(MyComponent,
        {
          panelClass: 'custom-dialog-container',
          data: { myObject: selectedObject},
          disableClose: true
        });

I inserted a breakpoint to confirm that selectedObject indeed contains all the fields and is not 'undefined'

In the constructor of my dialog component:

import { Component, OnInit, AfterViewInit,  Inject, ViewChild, ElementRef } from '@angular/core';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';
import { FormControl, Validators } from '@angular/forms';

    constructor(public dialogRef: MatDialogRef<InventoryRequiredInfoViewerComponent>,
        @Inject(MAT_DIALOG_DATA) public data: { myObject: any}) {
           
       var test = data.myObject;
    }

When I pause at this point, the value of data.myObject is coming up as undefined.

I have also included the dialog component in app.module.ts under both the declarations and entryComponents sections:

@NgModule({
  declarations:

and

entryComponents:

Despite going through several discussions, I am unable to pinpoint why the data object is failing to be passed correctly to the MatDialog. Any assistance would be greatly appreciated.

Answer №1

It appears that you have the capability to access the data, however, you may be unnecessarily redefining it within the constructor. You can try using this code instead: @Inject(MAT_DIALOG_DATA) data

Answer №2

Hey there! Here's an updated version of your code:

    const dialogSettings = new MatDialogConfig();

    dialogSettings.disableClose = true;
    dialogSettings.autoFocus = true;
    dialogSettings.maxWidth = '400px';
    dialogSettings.panelClass = 'custom-dialog-container';
    dialogSettings.data = {
      myObject: selectedObj,
    };

 let dialogRef = this.dialog.open(MyComponent, dialogSettings);

I hope this modification works for you!

Best regards

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

Angular8 does not recognize custom paths that have been added to the tsConfig file

Hey there! I'm facing an issue with Angular not recognizing a custom path I added to my tsConfig file. It seems like Angular already has a baseUrl property set to ./, starting from the current folder where the tsConfig file is located. The code snippe ...

What is the best way to retrieve the current complete URL in a Next.js/Typescript component?

I'm working on a component and I need to retrieve the current full URL. Here's a simplified version of what I have: /** * Share dropdown component */ export const ShareDropdown: React.FC<{ className: string }> = ({ className, }) => { ...

There are no call signatures available for the unspecified type when attempting to extract callable keys from a union

When attempting to write a legacy function within our codebase that invokes methods on certain objects while also handling errors, I encountered difficulty involving the accuracy of the return type. The existing solution outlined below is effective at cons ...

Implementing TypeScript/Angular client generation using Swagger/OpenAPI in the build pipeline

Generating Java/Spring Server-Stubs with swagger-codegen-maven-plugin In my Spring Boot Java project, I utilize the swagger-codegen-maven-plugin to automatically generate the server stubs for Spring MVC controller interfaces from my Swagger 2.0 api.yml fi ...

Contrasting `Function` with `(...args: any[]) => any`

Can you explain the difference between Function and (...args: any[]) => any? I recently discovered that Function cannot be assigned to (...args: any[]) => any. Why is that so puzzling? declare let foo: Function; declare let bar: (...args: an ...

Unable to assign value to a public variable in Angular

I am facing an issue where I am trying to retrieve a value from the localStorage and assign it to a variable. However, when I try to use that variable in functions, it is coming up as undefined. code export class DashboardService { public token: any; ...

What causes me to create components with incorrect paths?

Can someone assist me with creating a new component in the dynamic-print folder instead of it being created in the app? Thank you ...

Angular 2 approach to retrieving items from an Observable<Xyz[]>

After reviewing the Typescript code in an Angular 2 service: getLanguages () { return this.http.get(this._languagesUrl) .map(res => <Language[]> res.json().data) .catch(this.handleError); I'm encountering a challenge whe ...

A guide to troubleshooting and resolving the elusive 500 Server Error in Next JS API

Currently, I am in the process of developing a chat bot using the innovative OPEN AI GPT 4 Model within NextJS. However, upon sending a POST request to http://localhost:3001/api/generate, I am faced with a Response displaying a status code of 500 along wit ...

Develop a custom data structure by combining different key elements with diverse property values

Within my coding dilemma lies a Union of strings: type Keys = 'a' | 'b' | 'c' My desire is to craft an object type using these union keys, with the flexibility for assigned values in that type. The typical approach involves ...

Error: Unhandled promise rejection: Trying to access a property of null (specifically 'branch') causing a TypeError

While developing a dashboard for an Angular application, I encountered an error when trying to access the dashboard: ERROR Error: Uncaught (in promise): TypeError: Cannot read properties of null (reading 'branch') TypeError: Cannot read propert ...

Developing personalized modules in Angular version 6

I am diving into the world of Angular customization and I have a goal of developing a flexible module in Angular 6. My plan is to create this module and place it in the Node_modules folder so that it can be easily utilized by other teams within the organi ...

Step-by-Step Guide: Customize the Background Color in an Angular 4 Select Dropdown Based on the Selected Option

I'm facing a challenge in setting the background color of a select element based on the color attribute of its currently selected option. I'm using Angular 4 for this task. An example of what I am attempting to accomplish is shown below: <se ...

Checking and unchecking checkboxes based on certain conditions

Below are the checkboxes included in app.components.html <mat-checkbox (change)="temperature($bd_submain)">Temperature</mat-checkbox> <mat-checkbox color='primary'>Soil Moisture</mat-checkbox> When the "Temperature" chec ...

Ways to selectively deactivate client-side functionality

I have implemented a server-side rendered app with transitions, including a 404 error page that I placed in a lazy module to avoid increasing the size of loaded JavaScript. While this setup is functioning correctly, there is some flickering when the clien ...

Ensure Jest returns the accurate file paths for images in a TypeScript and React environment

I'm currently developing a React application and I have come across an issue with importing images. My usual method of importing images is as follows: import image1Src from 'assets/img1.png"; For testing purposes, I need to be able to make ...

In my coding project using Angular and Typescript, I am currently faced with the task of searching for a particular value within

I am facing an issue where I need to locate a value within an array of arrays, but the .find method is returning undefined. import { Component, OnInit } from '@angular/core'; import * as XLSX from 'xlsx'; import { ExcelSheetsService } f ...

TypeScript: implementing function overloading in an interface by extending another interface

I'm currently developing a Capacitor plugin and I'm in the process of defining possible event listeners for it. Previously, all the possible event listeners were included in one large interface within the same file: export interface Plugin { ...

Adding cache to Observable in mat-autocomplete can improve performance by reducing redundant API calls

I have successfully implemented a mat-autocomplete feature. However, I am facing an issue where the Http call is triggered with every keyup event, such as 'r', 'ra', 'ram', 'rame', 'ramesh'. This frequent u ...

Developing a collection of components with customizable color variations using React

I am interested in creating a React component library where users can specify color variants. For instance, I want to use the following syntax: const customTheme = createCustomTheme({ button: { variants: { primary: 'primary ...