Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to detect changes in child component with respect to changes in the base component – Angular

I have a component profile.component.ts in which there’s a variable, this.candidate, which subscribes to and gets updated like this

 this.candidateSub = this.store.select(selectCandidateState).subscribe((candidateState) => {
    this.candidate = candidateState;
 })

This component acts like a base component and there’s a child component that extends this component,

export class SkillsComponent extends profile implements OnInit, OnDestroy

Now I want to have another variable in this child component that updates itself whenever this.candidate gets updated in the base component. Something like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

this.candidateSkills = this.candidate.skills

The problem is I can’t get it done simply by declaring this.candidateSkills = this.candidate.skills in ngOnit as it doesn’t watch and listen to changes and update itself when this.candidate gets updated in base component.

How should I do it?

>Solution :

In parent component:

this.storeObservable = this.store.select(selectCandidateState)

this.candidateSub = this.storeObservable.subscribe((candidateState) => {
  this.candidate = candidateState;
})

In child component:

this.storeObservable.subscribe((candidateState) => {
  // do something
})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading