1

I'm Trying detect changes on child of variable, but ngOnChanges not firing when variable change

@Input() _parent: any;

ngOnChanges(changes: {[propKey: string] : SimpleChange}) { << not fire
  console.log('Change detected:');

  setTimeout(() => {

    if (!_.isEqual(this._parent.curScript, changes['_parent'].currentValue.curScript)) {
      console.log(1);
    }  
  }, 100);
}

What's my problem?

  • How change the variable is a input property of child component – alehn96 Jul 15 '17 at 03:10
  • ngOnchange is only for change in component and property of the component, not work with property of child component – alehn96 Jul 15 '17 at 03:18
  • 1
    Check this https://stackoverflow.com/questions/34796901/angular2-change-detection-ngonchanges-not-firing-for-nested-object?rq=1 – alehn96 Jul 15 '17 at 03:26

2 Answers2

0

As noted in the comments, ngOnChanges only detects changes on input properties, which are passed in when the component is composed into another component's template. The Angular docs for lifecycle hooks should be enlightening https://angular.io/guide/lifecycle-hooks#onchanges

Kai
  • 2,529
  • 1
  • 15
  • 24
  • sorry, i forgot my input definition before, my _parent was a Input: **@Input() _parent: any;** , but change happen only at the first time like ngOnInit –  Jul 15 '17 at 04:18
0

.you can detect a change via ChangeDetectorRef. you can use it like that

constructor( private dcr: ChangeDetectorRef) {
    }

this.dcr.detectChanges(); // put this line where changes made for example you change `_parent` variable value from parent component you need to put that place. child value changed automatically. does this help
Shailesh Ladumor
  • 7,052
  • 5
  • 42
  • 55