Tips for sending data from a child component to a parent component

I am facing an issue with my components setup. I have 3 components - 2 child components and 1 parent component. The parent component has a save button, and in order to get data from the child components in the parent component, I have implemented the following code:

<div class="border border-dark p-1 m-1">
  <basic-details
    *ngIf="customerData"
    [formType]="formType"
    [customerDetails]="customerData"
    #customerDetails
  ></basic-details>
</div>
<div class="border border-dark p-1 m-1">
  <verification-details
    *ngIf="customerData"
    [formType]="formType"
    [customerDetailsForKyc]="customerData"
    #customerDetailsForKyc
  ></verification-details>
</div>

<div class="row main justify-content-center">
  <button class="btn btn-primary" (click)="save(customerDetails, customerDetailsForKyc)">Save</button>
  <button class="btn btn-primary ml-2" (click)="cancel()">Cancel</button>
</div>

ts file

save(basic, kyc) {
    console.log('basic', basic);
    console.log('kyc', kyc);
  }

When I click on the Save button, I am able to retrieve proper data from the basic-details component in the parent component. However, for the verification-details component, I am encountering an error. Both child components are using @Input() to receive data like this:

for basic component:

@Input() customerDetails: ICustomerDetailsData;

for verification component

@Input() customerDetailsForKyc;

Am I missing something here?

Answer №1

I have successfully implemented your code with one parent component and two child components. I followed a similar approach to what you outlined in your initial query. One thing worth noting is that you did not specify the use of [formType]="formType", so I omitted it from my implementation. Feel free to review the code snippet below, which can be tested on StackBlitz: Angular two way data binding between parent and child co acqylp

import { Component, Input, Output, EventEmitter } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `<app-children [(data)]="message"  
              [customerDetails]="customerData"
              #customerDetails></app-children>
             <br><br>
             <app-children2 [(data)]="message2" 
              
             [customerDetailsForKyc]="customerData"
             #customerDetailsForKyc></app-children2>
            <div>Parent: {{message}}
            <br>
            <button class="btn btn-primary" (click)="save(customerDetails, customerDetailsForKyc)">Save</button>
            </div>`,
})

export class AppComponent {
  public message: string;
  public message2: string;
  customerData:ICustomerDetailsData={color:'TTT',width:15};
  ngOnInit() {
    this.message = 'Original message'
    this.message2 = 'Original message2'
  }
  save(basic, kyc) {
    console.log('basic', basic);
    console.log('kyc', kyc);
  }
}

@Component({
  selector: 'app-children',
  template: `<div>Children: {{data}}</div> 
              <br>
              <input type="text" class="form-control" id="name"
     
              [(ngModel)]="data" >
              <br>
             <a (click)="changeMessage('Children message')">Click me!</a>`
})

export class ChildComponent {
  @Input() public data: string;
  @Input() customerDetails: ICustomerDetailsData;
  @Output() dataChange= new EventEmitter<string>();
  changeMessage(message: string) {
    this.data = message;
    this.dataChange.emit(this.data);
  }
}

 @Component({
  selector: 'app-children2',
  template: `<div>Children: {{data}}</div> 
             <a (click)="changeMessage('Children message')">Click me!</a>`
})

export class ChildComponentNew {
    @Input() customerDetailsForKyc;
  @Input() public data: string;
  @Output() dataChange= new EventEmitter<string>();
  changeMessage(message: string) {
    this.data = message;
    this.dataChange.emit(this.data);
  }
}
interface ICustomerDetailsData {
  color?: string;
  width?: number;
}

Answer №2

If you need to pass data from a child component to its parent in Angular, one approach is to utilize the @ViewChild decorator to access the child component within the parent. This way, when an action like clicking a save button occurs, you can easily retrieve and work with the data from the child component. For more information, refer to Angular ViewChild documentation.

Answer №3

Here is the revised content:

  1. The approach you are using in your method, `save(customerDetails, customerDetailsForKyc)`, involves template variables as arguments. This practice is incorrect as template variables serve a different purpose. According to Angular documentation, "Template variables help you use data from one part of a template in another part of the template. With template variables, you can perform tasks such as respond to user input or finely tune your application's forms." Refer to Template variables | Angular for more information.
  2. If you intend to transfer data from the child component to the parent component, it is recommended to utilize the @Output decorator. For detailed guidance on sharing data between child and parent directives and components in Angular, visit Sharing data between child and parent directives and components | Angular.

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

Tips for Choosing a Tab in angular-ui: AngularJS

Is there a way to select the last tab without using ng-repeat? I want to avoid using ng-repeat but still need to select tabs. Any suggestions? If you'd like to see the code in action, you can visit: http://plnkr.co/edit/ZJNaAVDBrbr1JjooVMFj?p=preview ...

Dynamic background image that fills the entire webpage, adjusts to different screen sizes, and changes randomly

Currently, I am working on designing a web page with a dynamic background image that adjusts responsively in the browser without distortion. The challenge is to randomly select an image from an array using jQuery. Unfortunately, my knowledge of jQuery is ...

Creating a custom directive in Angular that dynamically applies attributes based on the current route

I have been able to successfully set the attribute data-toggle-state to both active and inactive using the following code: <a [routerLink]="['/home']" [attr.data-toggle-state]="router.isActive('/home', true) ? 'active' : ...

Is it possible to use a '.JS' file downloaded through Node Package Manager (npm) directly in a web browser?

Generally, I am looking to utilize a specific library without relying on Node CMD. For instance: I aim to create a TypeScript playground without having to execute 'tsc.cmd' from "npm\node_modules", instead, I want to directly call the tsc c ...

Angular: module instantiation unsuccessful

Just getting started with Angular and running into an issue where the module seems to be unavailable. https://docs.angularjs.org/error/$injector/nomod?p0=plopApp My code is very basic at the moment, just setting things up: @section scripts{ <script s ...

Dynamic JavaScript Animation

Check out this code snippet I currently have. Notice how the text seems to jump when rerun? Give it a try and see for yourself. The big question is: How can this be fixed? $("#aboutUsText").delay(1000).fadeOut(1000) $("#aboutUsText").attr("MyState", "1") ...

Tips for handling Firebase JS SDK errors within try-catch blocks

Attempting to type the err object in a function that saves documents to Firestore has been challenging. async function saveToFirestore(obj: SOME_OBJECT, collection: string, docId: string) { try { await firebase.firestore().collection(collection).doc( ...

Access the extended controller and call the core controller function without directly interacting with the core controller

i have a core controller that contains an array called vm.validationTypes with only 2 objects. I need to add 3 or 4 more objects to this array. to achieve this, i created another controller by extending the core controller: // CustomValidation angular.m ...

Tips on integrating ActiveX controls in Angular

I built my project using Angular 6 and TypeScript in Visual Studio Code. The browser being used is IE11. Unfortunately, when I try to run the code written in app.component.html, it doesn't work as expected. The HTML code causing the issue is: <d ...

Using TypeScript with Node.js: the module is declaring a component locally, but it is not being exported

Within my nodeJS application, I have organized a models and seeders folder. One of the files within this structure is address.model.ts where I have defined the following schema: export {}; const mongoose = require('mongoose'); const addressS ...

Utilizing Angular 2 to Pipe Values and Generate DOM Elements

I'm currently working on a custom pipe that will receive an integer value and then display a material design icon based on that integer. HTML: {{item.MetGoal | NumberToStatusSymbol}} TypeScript/JS: transform(value, args?) { switch (value){ ...

When an element in vue.js is selected using focus, it does not trigger re

One of my tasks involves tracking the last selected input in order to append a specific string or variable to it later on. created: function () { document.addEventListener('focusin', this.focusChanged); } focusChanged(event) { if (event ...

Unexpected Issue with Lightbox2 Functionality in Chrome Browser

Not too long ago, I reached out here for the first time with a similar issue: my image gallery on my art portfolio site was malfunctioning in Chrome, yet worked fine in Microsoft Internet Explorer and Edge. Thanks to some incredibly helpful and patient in ...

Is the ID "nodeName" specifically designated as reserved in the HTML5 language specifications?

I have an element with the following id: <span id="nodeName"></span> In my HTML code. Then, when using jQuery to do the following: $("#nodeName").html("someString"); I am getting an error in the console that says: Uncaught TypeError: Objec ...

displaying a PDF file in Safari

Is there a way to display a PDF document within an HTML page without encountering a missing plugin error? I attempted to use the following code, but the error persists. Interestingly, if I drag the same PDF file directly into my browser, it displays perfe ...

Retrieve server information without utilizing data.map since array.map is not a supported function in next.js version 13

Currently, I am developing a list page using next.js 13 to display a collection of projects. I made an attempt to enable server-side rendering for this feature. The implementation is located in app/team/page.tsx import { useRouter } from 'next/navig ...

The asynchronous nature of how setInterval operates

I am working with a setInterval function that executes asynchronous code to make calls to the server: setInterval(()=> { //run AJAX function here }, 5000); In scenarios where the server does not receive a response within 5 seconds, there is a like ...

Edit content on the fly using ajax (add information to database)

I have a question that pertains to both UX and technical aspects. I am currently creating a list using ajax and PHP. I am unsure whether it is advisable to manipulate the DOM before the backend processes are complete. What potential issues could arise if ...

What is the reason for not displaying the various li elements on my webpage?

Here is the code snippet export default function DisplaySearchResults({ results }) { var arr = Object.entries(results) console.log(arr) return ( <div> Here are the search results : <ol> {arr.map((va ...

Explore the feature of toggling visibility of components in Vue.js 3

I am facing an issue with showing/hiding my sidebar using a button in the navbar component. I have a navbar and a side bar as two separate components. Unfortunately, none of the methods I tried such as v-show, v-if, or using a localStorage value seem to wo ...