1
votes

Helo All,

I am facing a issue, as I am new in Angular so it may be a something I am missing.

In my application, in one of the child component ngOnChanges is getting called irrespective on any change, In below screen shot you will find all the currentValues and previousvalues properties of changes object is same.

Let me know if I am missing something.

Please also refer below code

Thanks

import { Component, Input, SimpleChanges, OnInit} from '@angular/core'; import { OnChanges, OnDestroy } from @angular/core/src/metadata/lifecycle_hooks';

@Component({
selector: 'xxxxxx',
templateUrl: './result.component.html',
styleUrls: ['./result.component.css']    

})

export class ResultComponent implements OnInit, OnChanges, OnDestroy {


@Input()
searchResults: SearchResults.SearchResult;

@Input()
searchText: string;

@Input()
loading: boolean = false;

ngOnInit() {
    var logger = "stop me debugger";
}


ngOnChanges(changes: SimpleChanges) {

    if (changes.searchResults && !changes.searchResults.firstChange) {


    }
}


ngOnDestroy(): void {
    var logger = "stop me debugger";
}



constructor() {


}

}

enter image description here

1
add code instead of image - Chellappan வ
There's nothing in your provided code fragment that should lead to this ngOnChanges call. - Christoph Lütjen

1 Answers

0
votes

The lifecycle hook ngOnChanges:

Respond when Angular (re)sets data-bound input properties. The method receives a SimpleChanges object of current and previous property values.
Called before ngOnInit() and whenever one or more data-bound input properties change.

Angular Lifecycle Hooks

Because of this and some of your properties are inputs, ngOnChanges gets called. That's also why the previousValue and the currentValue of the SimpleChanges object are undefined.