Vishwajeet
Vishwajeet

Reputation: 1683

Angular 2 ngOnchange event is getting called irrective of any change

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

Upvotes: 1

Views: 1822

Answers (1)

mahalde
mahalde

Reputation: 709

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.

Upvotes: 0

Related Questions