ETA: I am aware of different methods for monitoring my form for alterations. However, that is not the focus of my inquiry. As indicated by the title, my question pertains to observing changes within an object. The application displayed below is solely for demonstration purposes. Kindly provide a response to the specific query I have posed. Thank you!
Presented here is a straightforward app:
import { Component, OnInit } from '@angular/core';
export class Customer {
firstName: string;
favoriteColor: string;
}
@Component({
selector: 'my-app',
template: `
<div *ngIf="customer">
<input type="text" [(ngModel)]="customer.firstName">
<input type="text" [(ngModel)]="customer.favoriteColor">
</div>
`
})
export class AppComponent implements OnInit {
private customer: Customer;
ngOnInit(): void {
this.customer = new Customer();
// TODO: How can I establish a callback function that triggers whenever
// any property of this.customer undergoes modification?
}
}
Note the pending task. I am seeking to create a subscription mechanism that activates each time any property of this.customer
is altered.
I am unable to utilize ngChange on the input elements. My requirement is to directly monitor changes within the model. The rationales behind this decision are specific to my project and will not be elaborated upon here. Please understand that this limitation is non-negotiable.
Is there a solution to this? Despite extensive online research efforts, I have yet to find a suitable answer.