Keep an ear out for updates on object changes in Angular

One of my challenges involves a form that directly updates an object in the following manner:

component.html

<input type="text" [(ngModel)]="user.name" />
<input type="text" [(ngModel)]="user.surname" />
<input type="text" [(ngModel)]="user.age" />

component.ts

user = {
  name: null,
  surname: null,
  age: null
}

Within my component.html, there is a save button implemented in a child component.

<app-save-btn [user]="user"></app-save-btn>

In the app-save-btn component, I am utilizing OnChanges to detect changes.

@Input() user: User;

userForCompare: User;

ngOnChanges() {
  if (!this.userForCompare) {
    this.userForCompare = {...this.user};
  }
  console.log(this.userForCompare, this.user);
}

My issue arises when I update additional fields beyond the initial ones - I do not receive updated change information.

While I understand that I could create individual variables for each field, as I have approximately 30 fields, I would prefer to keep my current implementation relatively unchanged...

Any assistance or guidance on this matter would be greatly appreciated.

Answer №1

@Input property will only activate the ngOnChanges function if the Input object is modified.

In this scenario, each input field modifies a different property within the same user object. Even though the child properties (name, surname, and age) of the user object are changed, the ngOnChanges function will not be triggered because the @Input always points to the same object.

As a solution, you need to assign a completely new user object to this.user in the component.ts file whenever any input field is altered.

I hope this explanation clarifies things for you!

Answer №2

If you want to efficiently manipulate your form fields, I recommend creating a FormGroup using Reactive Forms. By utilizing FormBuilder.group(), you can easily create a form that includes properties of the User object. This way, you only need to pass form.value to your child component.

Reactive forms maintain the purity of the data model by treating it as an immutable data structure. Whenever changes are made to the form, the FormControl instance does not update the existing data model; instead, it returns a new state (a new data model).

You can see a working example on StackBlitz with the following code:
https://stackblitz.com/edit/angular-stackoverflow-60424523

example.component.html

<form [formGroup]="form">
  <input [formControlName]="'name'" />
  <input [formControlName]="'surname'" />
  <input [formControlName]="'age'" />
</form>
<app-save-btn [user]="form.value"></app-save-btn>

example.component.ts

import { Component, OnInit} from '@angular/core';
import { FormBuilder, FormGroup} from '@angular/forms';

@Component({ ... })
export class AppComponent implements OnInit {

  form: FormGroup;

  user: User = {
    name: 'Initial name',
    surname: 'Initial surname',
    age: 99
  };

  constructor(
    private formBuilder: FormBuilder,
  ) { }

  ngOnInit() {
    this.form = this.formBuilder.group(this.user);
  }
}

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

Scrolling to specific ID scrolls only in a downward direction

I have been using fullpage.js for my website and I am facing an issue. When I create a link and connect it to an id, everything works perfectly. However, I am unable to scroll back up once I have scrolled down. Here is the HTML code: <a href="#section ...

When utilizing Angular CDK virtual scroller, an error occurs stating that @angular/core/core does not have an exported member 'ɵɵFactoryDeclaration'. Can anyone explain why this is happening

After adding CDK Virtual Scroller to my ionic 5.3.3 project using the command: npm add @angular/cdk The version installed is: "@angular/cdk": "^13.0.2" The scroller viewport contains an ion-item-group: <ng-template #showContentBlo ...

Understanding the process of reading cookies on the Angular2 side that have been set by the C# server

I'm struggling to understand how the angular code can access the cookie that was set on the server side. I found the following code snippet on GitHub. This C# function sets the cookie in the Response object with the last line of code. My question is, ...

Ways to Implement an Origin's First-Party Cookies While Using it as a Third-Party

Imagine I am the owner of a website called "treat1creator.com". I want my clients, who are also website owners, to send HTTP requests to my server that contain cookies I have generated. Here is how it works: User "Sally" visits my site at treat1creator.co ...

Guide on comparing an object against an array and retrieving a specific output

If I were to create a data structure like this: const carObj = {"1234":"Corvette","4321":"Subaru","8891":"Volvo"}; And also have an array that contains the IDs: const myArray = [1234, 4321, 8891, ...

What is the best way to divide a string into an array containing both linked and non-linked elements?

I'm struggling to find the right solution to my problem. I need to create a view that is enclosed in a clickable div. The content will consist of plain text mixed with clickable URLs - the issue arises when clicking on a link also triggers the method ...

Incorporating a delay into looped HTTP requests while effectively utilizing Promise.all to track their completion

Greetings! In my current project, I am trying to introduce a 50ms delay before each subsequent HTTP request is sent to the server. Additionally, I aim to incorporate a functionality that triggers after all requests have been successfully made. To better e ...

What is the method to retrieve values passed to the next() function in Node.js?

For my current project, I am utilizing Node.js in combination with Express.js to develop the back-end. In middleware functions, next() is commonly used to progress through the chain until reaching the final app.VERB() function. My question is, at what poi ...

Creating a Star Rating system with jQuery using a dropdown menu for multiple selections

Recently, I came across a simple jQuery Star rating system that I want to implement on my website. I have been following the code from http://jsfiddle.net/, and it works perfectly for one star rating system. However, I have multiple star rating systems on ...

Guide on converting arrays and sending this information in Node.js

I managed to retrieve quiz data and now I want to enhance it by mapping each answer to its respective quiz. I have a feeling that I should use something like forEach.push, but I haven't quite figured out the exact method... const API_KEY="https: ...

Struggling to set up a Jest testing module for a NestJs service. Encountering an issue where Nest is unable to resolve dependencies of the UsersService, specifically the Config

Greetings, fellow developers! I am excited to ask my first set of questions on stackoverflow :) Currently, I am working on a test/learning application to enhance my skills in NestJS and Vue. During the implementation of server-side unit tests using Jest, ...

Loading website with continuous HTML5 video playback

I'm currently working on a website that features a large section with a video background set to loop. To enhance the user experience, we decided to include a preloading image while the site loads its resources. Our current setup involves using jQuery ...

What could be causing the .hover function to malfunction and how can I make it so that the .hover function only applies within the corner radius area?

I am attempting to make circles react to my jquery .hover function. Below is the JavaScript code I am using: jQuery.fn.center = function () { this.css("position","absolute"); this.css("top", Math.max(0, (($(window).height() - this.outerHeight()) / 2) + ...

Angular 4's Panel Window: A User-Friendly Interface

Having experience with Adobe Flex, I am familiar with creating new panel windows in Action Script by using the 'new' keyword and popping them up using popups. The customization of these windows was achieved through functions provided in the Panel ...

How can I pass an argument to Node within a package.json script instead of the script itself?

In my project's package.json, I have a script that looks like this: "scripts": { "test": "... && mocha --full-trace test/db test/http test/storage test/utils", I am trying to pass the --async-stack-traces o ...

Managing various encoding methods when retrieving the XML data feed

I'm attempting to access the feed from the following URL: http://www.chinanews.com/rss/scroll-news.xml using the request module. However, the content I receive appears garbled with characters like ʷ)(й)޹. Upon inspecting the XML, I noticed that ...

How can I select the specific element within a class when a particular checkbox is selected?

Having a dynamically generated list of elements, each structured like this: <div class="under-item-description"> <span class="under-compare-price">100</span><span class="under-price">50</span> <span class="under-compar ...

Troubleshooting React: Child Component Fails to Render

Currently, I am enrolled in a React course and deep diving into the lifecycle methods of React. Through implementing componentDidMount, I have successfully made API calls and set the state accordingly. However, I am facing an issue with displaying images ...

Establish the state as the result of a function

I need to update the state of timeToCountdown with the value stored in allTimeInSeconds. Next, I intend to pass this data as a prop to a component. class Timer extends Component { constructor(props){ super(props); this.state = { ...

Using Node.js to Merge the Responses of Two Results

Here is the code snippet for fetching a list of users associated with a particular ID from this service.ts file. async getId(id: number) { // Code to retrieve participants list based on ID let participants = await this.participantRepo.findById( ...