Child component does not detect changes in @Input array

I am working with an angular2 parent component that looks like this:

ParentComponent {

   some_array : Array;

   (...)

}

Its child component is structured as follows:

ChildComponent {

     selector: "child"
     @Input some_object : Object;

}

In my layout, starting from the Parent component:

<ng-container *ngFor="let object of some_array">
     <child [some_object]="object></child>
</ng-container>

And for the Child layout:

<Text [text]="some_object.text"/>

The rendering of the view is flawless and everything works perfectly.

However, when I make changes to my object property within the some_array in the parent component using a for loop with [i]ndex, the child component does not reflect these changes and I need a way to detect them.

What would be the best approach to achieve this?

Thank you for your assistance

Answer №1

If you haven't specifically set OnPush as the ChangeDetectionStrategy or completely detached from ChangeDetection, the changes should still reflect in your child component.

But if you're wondering how to react to changes in data-bound properties in general, you can use the OnChanges lifecycle hook.

export class ChildComponent implements OnChanges {

   ngOnChanges(changes: SimpleChanges) {

     if (changes['some_object']) {
       //
       // React to changes in a general manner
       // or access changes['some_object'].currentValue
       // or changes['some_object'].previousValue
       // ...
     }
   }
}

I hope this insight helps you resolve your issue!

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

I seem to be encountering an issue with my Angular 6 HTTP PUT request

Below is the code snippet: products.service: updateCategorie(ucategorie: Icategorie) { const endpoint = this.url + 'api/Category/Edit'; const headers = new Headers(); headers.append('Authorization', 'Bearer ' + localStorage ...

Are Angular 4 auth guards implemented on the server side or the client side? And if they are on the client side, are they vulnerable to

While I am engaged in a project using Angular 4, my expertise lies in angular auth-guards. Here's my query: considering that Angular 4 is primarily a client-sided framework, is it possible to bypass the auth-guard by inspecting the browser window, giv ...

What are the reasons for being unable to utilize display flex within Angular?

Recently delving into Angular, I have been trying to apply display flex to <mat-form-field> without success. I have scoured Google for answers, but still haven't found a solution. Here is my HTML: <form [formGroup]="searchForm" (n ...

"A loader specifically designed for this type of file is necessary," webpack is unable to interpret the angular2 file

I am encountering an issue while setting up a basic Angular2 app with Webpack as the module bundler. I am following the guidelines provided in this code repository and have configured all files accordingly, making necessary changes to file paths. However, ...

"Unfortunately, this container did not send out any hits" - Google Tag Manager

After successfully integrating Google Tag Manager into my Next.js website, here is the implemented code: import '../styles/global.css'; import type { AppProps } from 'next/app'; import Script from 'next/script'; import NextNP ...

"Implementing a loop to dynamically add elements in TypeScript

During the loop session, I am able to retrieve data but once outside the loop, I am unable to log it. fetchDetails(){ this.retrieveData().subscribe(data => { console.log(data); this.data = data; for (var k of this.data){ // conso ...

What is the best way to retrieve the overall count of a field within a reactive form, while also identifying both the valid

In the form I am working on, some fields are required while others are not. I am trying to figure out how many total fields there are and how many have been filled correctly without breaking any validation rules, even if they are not marked as required. I ...

Leverage function calls within Angular template code

I'm currently working with Angular 8 and trying to implement a function that accepts parameters in Angular templates using PipeTransform. I encountered an issue where the variable becomes undefined outside of the subscribe method when calling a servic ...

Retrieve the values stored under the "kilos" key for every object in the dataset

Recently, I stumbled upon the following code snippet: data = [ 0: {kilos: 10}, 1: {other:1, kilos: 5}, 2: {other:2, kilos:6} ] After analyzing it, I realized that I need to extract all the values associated with the "kilos" keys and then calculat ...

Retrieve the thousand separator for numbers using Angular in various languages

When using the English locale, numbers appear as follows: 111,111,222.00, with a comma as the thousand separator and a point as the decimal separator. In languages like German, the same number would be represented as 111.111.222,00, reversing the positions ...

What is the process for sending JSON data in a file to a django rest api?

Currently, I am using Angular on the front-end and Django Rest on the back-end. I have encountered a scenario where I need to create a complex model. Despite considering other simpler solutions, I believe that using JSON to pass the files can streamline th ...

Issue with PrimeNG p-editor Appearance

My text editor is not displaying correctly on my website. Please refer to the following images for reference: Top of the page Bottom of the page Currently, it only shows a large SVG and a few input fields at the bottom. The technologies I am using includ ...

Can Angular tests be used to test a component that is a grandchild in the component hierarchy?

Currently, we are utilizing karma testing to verify the presence of buttons in a component. The challenge is that the component presents the buttons as children of another child. What we are examining is: ProductNavComponent \ NavComponent &bsol ...

Exploring the usage of asynchronous providers in NestJS

I am currently utilizing nestjs and I am interested in creating an async provider. Below is my folder structure: . ├── dist │ └── main.js ├── libs │ └── dma │ ├── src │ │ ├── client │ ...

Is Angular 4 failing to set headers properly or is Express.js searching in the wrong place?

When interacting with an Express.js API, I encountered a issue regarding the handling of auth tokens. The problem arose when sending the token in the request headers using Angular 4 compared to Postman. In Postman, setting the header named 'Authorizat ...

What is the best method for retrieving an item from localstorage?

Seeking advice on how to retrieve an item from local storage in next.js without causing a page rerender. Here is the code snippet I am currently using: import { ThemeProvider } from "@material-ui/core"; import { FC, useEffect, useState } from "react"; i ...

Troubleshooting Problem with Dependency Injection in Angular 2 Services

Within my application, there is a Module that consists of two components: ListComponent and DetailsComponent, as well as a service called MyModuleService. Whenever a user visits the ListComponent, I retrieve the list from the server and store it in the Li ...

AsyncPipe encounters an InvalidPipeArgument, signaling a pipe error

I've been exploring Async Pipe and seem to be stuck at this particular point... import { Component, OnDestroy } from '@angular/core'; import { AngularFireDatabase } from 'angularfire2/database'; @Component({ selector: 'a ...

Ways to sort mat-select in alphabetical order with conditional names in options

I am looking to alphabetically order a mat-select element in my Angular project. <mat-select [(ngModel)]="item" name="item" (selectionChange)="changeIdentificationOptions($event)"> <mat-option *ngFor="let item of items" [value]="item"> ...

The program encountered an error with code TS2339, indicating that the property "name" cannot be found on the type "never"

app.component.html I'm attempting to display the response data from my Firebase project using *ngFor. <div class="row"> <div class="col-md-3"> <h4 class="text-warning">All Employee Da ...