Experiencing difficulty in transferring array information from a parent component to a child component within an

I'm currently working on a task where I need to pass data from a parent component to a child component. The data consists of an array that is nested within another array.

parent.component.html

<div *ngFor="let parent of parentArray; index as mainIdx" class="main-container" #mainDiv>
  <button (click)="addChild(mainIdx)"><i class="fa fa-plus"></i></button>
  <button (click)="addSibling(mainIdx)"><i class="fa fa-plus"></i></button>
  <button (click)="removeSibling(mainIdx)"><i class="fa fa-minus"></i></button>
  <app-child  *ngFor="let child of parent.childArray" [childData]="parent.childArray"></app- 
  child>
 </div>

parent.component.ts

addChild(parentIndex: number){
  
   let childId: number;
  if(this.parentArray[pIndex].childArray === undefined){
     this.parentArray[pIndex]['childArray'] = [];
     this.parentArray[pIndex].childArray.push({id: 0})
  }
  else{
    childId =  this.parentArray[pIndex].childArray.length;
    this.parentArray[pIndex].childArray.push({id: childId})
  }
 } 

child.component.ts

@Input() childData: any[];
    
   ngOnInit(): void {
    console.log(this.childData);
  }

   addGrandChild(){
    this.childAdded = true;
  }

child.component.html

<div class="child-container">
  <button (click)="addGrandChild()"><i class="fa fa-plus"></i></button>
  <button (click)="removeChild()"><i class="fa fa-minus"></i></button>
  <app-child *ngIf="childAdded"></app-child>
</div>

I am striving to create a tree-like structure with dynamically generated children and grandchildren. Each parent can have siblings and children, while each child can have grandchildren, continuing on indefinitely. I have successfully generated the components, but there seems to be an issue with passing data from the parent to the child.

Answer №1

Implement ngOnChanges

ngOnChanges(changes: SimpleChanges): void {
  console.log(changes.childData.currentValue);
}

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

Enhance Tuple in Angular Component Properties

When using React, state variables can be created like this: const SomeComponent = ({ someProp }) => { const [value, setValue] = useState<boolean>(false); } I wonder if there is a similar way to achieve the spread of a tuple within an Angular co ...

Generate some text on the following page utilizing the JavaScript function window.print()

Within my ASP.NET MVC View, I included this code snippet to display the content of my webpage: var newWindow = window.open(); newWindow.document.write(); newWindow.document.write(document.getElementById("sbmtform").innerHTML); newWindow.print(); I also ...

modify the color of a particular div element in real-time

I am looking to dynamically change the color of the divs based on values from my database. Here is what I have so far: Database: shape id is_success id1 0 id2 1 id3 0 id4 1 <div class="container" style="background: black; widt ...

Incorporating a requirement into a function to ensure it is executed only under certain circumstances

I'm currently developing a text-based game using JavaScript that involves picking up a sword with the help of an in-game function. var takeSword = function() { if (currentRoom.hasSword) { $("<p>You picked up a sword.</p&g ...

What is the best way to convert an array of data into a dataset format in React Native?

Within my specific use case, I am seeking to reform the array structure prior to loading it into a line chart. In this context, the props received are as follows: const data = [26.727, 26.952, 12.132, 25.933, 12.151, 28.492, 12.134, 26.191] The objective ...

I had high hopes that TypeScript's automatic type inference for constructor parameters would do the trick, but it seems to have let

You can experiment with the given code snippet at the online playground to confirm it. Consider this code: class Alpha { private beta; constructor(b: Beta) { this.beta = b; } doSomething() { ...

Having trouble with Ionic on Safari and iOS 11 when using the ServiceStack Client

My Ionic app is encountering issues when calling any webservice method (ServiceStack) on Safari 11.1 (13605.1.33.1.2). The same problem occurs when running on iOS 11 device or simulator, as shown in the attached image. The app functions properly on Chrome ...

Utilizing Vue to link data to an input field

I am struggling with a loop of input elements <li v-for="role in ..."> {{-some html-}} </li> Each input is nested inside the loop as shown below <input type="text" :data-some="role.name" :value="role.name" name="name" ....> While ...

Efficient method of delivering cohesive information from database to user without the need for continuous querying

Within the database, there is stored data about each user that typically remains constant. However, occasionally a user may update their information, such as changing their name. This data includes the user's name, username, and company details. The ...

Why are the icon pictures not displaying in Angular's mat-icon-button?

Recently, I stumbled upon a snippet of code in an Angular project that caught my eye. Naturally, I decided to incorporate it into my own program: <div style="text-align:center"> <a mat-icon-button class="btn-google-plus" href="http://google.com ...

Typescript ensures that the return type of a function is a key of an interface, which is determined based

I am attempting to enforce a specific return type from a function based on the key passed to it. For example, if the key is service1, then the correct return type should be Payloads['service1']. How can I accomplish this? interface Payloads { ...

The error message "element is not defined" is indicating an issue related to the cordova-plugin-google

In my current project using Ionic 3, I decided to implement map-related features by incorporating the Google Maps plugin recommended by the Ionic Team. This specific plugin serves as a wrapper around cordova-plugin-googlemaps. Following the steps outlined ...

Transform Promise-based code to use async/await

I'm attempting to rephrase this code using the async \ await syntax: public loadData(id: string): void { this.loadDataAsync() .then((data: any): void => { // Perform actions with data }) .catch((ex): v ...

The specified starting point "@angular/material/card" cannot be accessed due to missing dependencies

When attempting to deploy a web application on Heroku, I encountered the following error message: - Generating browser application bundles (phase: setup)... Compiling @angular/core : es2015 as esm2015 Compiling @angular/common : es2015 as esm2015 Compiling ...

What are some ways I can showcase the outcomes of my multiple choice quiz?

I need help with my multiple choice quiz. I want to display the answers under each question and highlight the correct one in bold. Here is the current code: function check() { var question1 = document.quiz.question1.value; var question2 = document.q ...

The function cloneElement does not share any properties with the type Partial<P> and Attributes

I'm encountering a perplexing issue with my code. When I attempt to call cloneElement with the second parameter being of type Type { foo: number } has no properties in common with type 'Partial<Child> & Attributes', TypeScript thro ...

Utilize external functions in evaluated code

After working with a TypeScript file containing the following code: import { functionTest } from './function_test' function runnerFunctionTest() { console.log("Test"); } export class Runner { run(source : string) { eva ...

What could be causing the ExcelJs plugin to malfunction in Internet Explorer 11?

My current setup involves Angular 9 and excelJs 4.1.1, which works perfectly in Chrome but throws an error in IE11 stating: "Invalid range in character set" in polyfills-es5.js Surprisingly, when I remove this dependency from package.json, everything func ...

Spring-boot does not support the content type 'application/form-data', resulting in an HttpMediaTypeNotSupportedException

Once I had set up the configuration files application.yml, application-dev.yml, application-prod.yml in Jhipster: spring: application: name: xxx http: multipart: enabled: true max-file-size: 200MB ...

Designated location for downloading specified by the user

I've been searching for a while now and I can't seem to find a satisfactory answer. Essentially, I have a website where users can input values and download a specific file based on those values. Everything is functional, but I want to give the u ...