2

I have 5 dynamic objects out of those 5, I am referring to only 2 here:

  1. objCache.tab1.page1.status: when cache enable from tab
  2. objCache.page1.status: when cache enable from page

In my html and .ts file, I will use one of the above objects dynamically based on my cache level.

I am passing status as input parameter "receivedStatus" to my child component as below, where right side value (xyz...) will be either from object 1 or object 2 from above 2 dynamic objects based on cache level. I don't want to use if or switch condition in html for dynamic objects.

<child-component [receivedStatus]="xyz..."></child-component>

As in above syntax the right hand side assigned value is dynamic, child component input parameter don't detect value change event.

Is there any other way to track status change event only when status changed, I don't want any function which repeatedly call in DOM, it should call only when dynamic object status value got changed.

2 Answers 2

0

Assuming the property is xyz which contains the complex object.

<child-component [receivedStatus]="xyz"></child-component>

All you need to do, is create a new memory reference (Remember objects and arrays (non-primitives) are stored as memory references and not actual values, so updates to the inner properties do not affect the memory reference.

So, we need to create a new memory reference using array/object destructuring, perform this after the update operations are done to xyz.

updateOperation() {
  this.xyz.tab1.page1.status = true;
  this.xyz = { ...this.xyz }; // use destructuring to create a new memory reference.
}

When you create the new memory reference the child component will detect that one of the inputs has changed.

You have to place the logic that modifies the component based on the new input, in the ngOnChanges hook, which fires only when the input bindings change.

ngOnChanges() {
  ...
  // logic to update the component based on inputs
  ...
}
Sign up to request clarification or add additional context in comments.

3 Comments

my object can be any from your assumption like this.xyz.tab1.page1.status OR this.xyz.page1.status OR this.status updateOperation() { this.xyz.tab1.page1.status = true; this.xyz = { ...this.xyz }; // use destructuring to create a new memory reference. }
@Sandip-FrontendDeveloper as long as memory reference is updated, ngOnChanges fires then we can update based on the new input using ngOnChanges
@Sandip-FrontendDeveloper If it is a individual property (this.status), pass it separately as a @input property.
0

You can use @Input() setter or ngOnChanges

Ex

export class ChildComponent {
  private _status: any;

    @Input()
    set receivedStatus(value: any) {
        if (value !== this._status) {
            this._status = value;
            this.onStatusChanged(value);
        }
    }
    get receivedStatus() { return this._status; }

    private onStatusChanged(newVal: any) {
    // handle it here
    console.log('new status', newVal);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.