Sending data back to the parent page when closing a child component popup in Angular is a common task. Here's a guide on how to

I have a specific goal that I am trying to achieve:

  1. When the user clicks on a Button on the Parent page, a Child Component in the form of a popup should open.
  2. Upon saving data on the child popup component, I need to pass this data from the Child component to the Parent.
  3. I have implemented Emit() to pass the data, and it is working when triggered by ngOnInit(), but not when the save button is clicked on the Child component.

Parent Component HTML-

<parentComponent>
 <app-upsert-service-pop-up *ngIf="this.AddService [childProperty]="this.parentProperty"
  (onInitEvent)="receiveAutoMsgHandler($event)"
  (savedService)="serviceChangedHandler($event)">
 </app-upsert-service-pop-up>
</parentComponent>

TS-

 serviceChangedHandler(data: any) {
    console.log("Data from CHild component--");
    this.childServiceDataChange  = data;
    console.log("Data from CHild component--",data);
  }
  receiveAutoMsgHandler(p) {
    this.msgOnChildCompInit = p;
    console.log(p);
  }

CHILD-

@Output() savedService =new EventEmitter();
 @Output() onInitEvent = new EventEmitter();
 ngOnInit() {
    this.onInitEvent.emit('This message automatically appears as the child component initializes. It is defined in the child component's ngOnInit method.');}
 onSaveServerDetails() {
    this.savedService.emit('After saving data');}

The onInitEvent is functioning correctly, but the similar code for savedServices is not working as expected.

I'm unsure why this is happening - could it be due to the child component being a popup or am I missing something?

Answer №1

In most cases, the common practice I've followed and observed is for the main page to pass a callback function to the dialog component.

Once the 'submit' button in the dialog is clicked, the dialog triggers the parent's callback function, passing the data as an argument, and then closes itself.

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

Configuring eslint-import-resolver-typescript in a multi-package repository

I'm currently working on implementing path-mapping within a monorepo structure. Despite having existing eslint-plugin-import rules in place, I am encountering an error stating "Unable to resolve path to module" for all mapped imports. app/ ├─ pack ...

The functionality of Silex Middleware Authentication varies depending on whether it is being used in Postman or

As a developer with limited experience in back-end development, I have encountered a real problem despite my best efforts. I am currently using Silex as a simple backend for an Angular 2 App. I have implemented a middleware that checks whether the user ha ...

Unable to perform a default import in Angular 9 version

I made adjustments to tsconfig.json by adding the following properties: "esModuleInterop": true, "allowSyntheticDefaultImports": true, This was done in order to successfully import an npm package using import * as ms from "ms"; Despite these changes, I ...

Verify registration by sending an email to an alternate email address through Angular Firebase

I have implemented email verification for users before registration. However, I would like to receive a verification email to my own email address in order to finalize the registration process. I want to be notified via email and only after my approval sho ...

Distinguishing keyboard and mouse events while focusing in React app

I have been working on implementing keyboard navigation focus outline for accessibility. The pseudo class :focus-visible works well on all elements except for input elements like text or textarea. It seems that inputs always have this pseudo class active s ...

Guide on redirecting a non-hash URL to a URL with hash in Angular

Upon receiving a URL, it appears as http://myapp.com/foo. However, due to the Angular app routing working "with hash", the correct component can be accessed at http://myapp.com/#/foo. With a defined route like: { path: '', redirectTo: ' ...

Unveiling a node through code in the angular tree component

Struggling to figure out a simple task using the angular-tree-component - given how amazing the component is, I'm sure it's something straightforward in the API that I am missing. I am trying to dynamically display a node by expanding all its pa ...

Angular and D3.js: Enhancing StackedBar Chart ToolTips with Added Functionality

I have modified the code from this example to be compatible with the latest versions of Angular and D3. Although it works well after a small tweak, I am unable to see the tooltips when hovering over the chart... https://i.sstatic.net/Rpebe.png <h3> ...

"Exploring Angular 9: A guide to retrieving form data with an array of objects [Revised as of July 29th, 2020

I am encountering an issue with my Angular 9 form code. I am getting the error "ERROR TypeError: Cannot read property 'mobile_number' of undefined" and I need help in resolving this problem. <form (ngSubmit)="processForm()"> & ...

The Angular application on port 4200 is displaying information using HTML, while the NodeJS server on port 8080 is showcasing data in

I am currently utilizing angular 5 along with node js to create a crud application for events data. The issue I am facing is that when I try to retrieve events data from port 4200 (http://localhost:4200/event) which is being served through angular, everyth ...

Having trouble with my first Angular 2 application that is meant to print "My First Angular 2 App"?

I have created a program to display the message "My First Angular 2 App", but I am facing issues with the output. Can anyone help me identify where I might be going wrong? environment_app.component.ts import { AnotherComponent } from './anotherCompo ...

Send an object containing client values to the parent component upon initialization in Angular

I am trying to pass a set of values to the parent component when a child component is loaded. However, I seem to be having trouble with this process. Any assistance or guidance on what might be going wrong would be highly appreciated. Here is the "data cl ...

Commitment and follow through

I am encountering an issue in my Angular2 (ionic2) application. There seems to be a problem with the "subscribe" property not existing on this.cityService.getAllCities(). Within cityPage.ts, there is a function structured like this: getCities(){ this ...

The NG Bootstrap table is not displaying any data from Angular service once it has been called

I have been using the Angular Bootstrap library for my application development. Specifically, I utilized the Angular Bootstrap Table to create a page that displays sortable and searchable information. To implement this, I referred to the example provided i ...

Understanding TypeScript: Utilizing type intersection and the powerful 'this' keyword

Can you explain the contrast between the following: interface MyType { f<T>(other: T): this & T; } versus interface MyType { f<T>(other: T): MyType & T; } ? Your insights would be highly appreciated! ...

An issue has been detected with the width attribute in Typescript when using

I have a question regarding the TypeScript error related to the width property. I've created a component called ProgressBar where I'm using Stitches for styling. However, TypeScript is throwing an error even when I specify the type as ANY. impor ...

Steps for integrating third party modules into an Angular library built with the CLI

Looking to integrate Pure.css module into an Angular library developed with the CLI's generate library command. In my workspace app, I used the generate library command to create a new library. After successfully creating and linking it to another pr ...

Issue with accessing class property in events.subscribe in Ionic3

I am currently working on a class that listens for events. When the event is triggered, I need to add the data that accompanies it to an array and then display it. Here's what my class looks like: export class customClass { dataArray:Array<stri ...

Error in TypeScript: It is not possible to use a component with MUI styling as a JSX element

I can't figure out what's going wrong. I'm attempting to include a child component in the main page, and I have a feeling it has something to do with MUI styles being applied at the end. I removed all unnecessary code and still encounter thi ...

Connect your Angular project on your local system

Having some trouble connecting my angular 13 project as a ui-component library. Successfully built the project and have the package stored in a dist folder. I then linked it to my primary project (angular 14) using "yarn link" and confirmed its presence as ...