Home > Blockchain >  Detect changes in observable using async
Detect changes in observable using async

Time:08-27

I am trying to call function init() when person property change. I am using async pipe in html so I want to run init function whenever their is any change in person object.

export class AppComponent implements OnInit {

  person: Observable= {fname: 'sam', lname: 'xyz'}; // this is sample value given
  
  ngOnInit() {
this.initFunc()
  }

initFunc(){
console.log(this.person.fname)
}

}

CodePudding user response:

you need to subscribe observable in order to execute some code each time something new is emitted. Not sure also init the observable in that way is correct, you can use the of(object) operator to emit the value one time. You can see here some example: enter link description here

CodePudding user response:

There is an ng hook called ngOnChanges for such situation this is called whenever there is a change detected

Or if u want to check detections then you can use ChangeDetectorRef and use the predefined detechChanges()

ChangeDetectorRef

  • Related