venkataramana govindu
venkataramana govindu

Reputation: 21

specific use case for ngDocheck and ngOnchanges

Is there any specific use case for ngDocheck and ngOnchanges. I read this blog and I got to know that the framework uses these events in change detection and all. Is there any specific scenario where we use these events please help me in understanding this. Thanks in advance 🙂.

Upvotes: 0

Views: 1060

Answers (1)

skolldev
skolldev

Reputation: 1197

Let's say you have an important input parameter in your component that determines the logic to be used. When this parameter changes, you want to call initialization logic.

export class Component implements OnChanges {
    ngOnChanges(changes: SimpleChanges): void {
        if (changes.importantParameter) {
            this.initialize();
        }
    }

    constructor(private api: API) {}

    public initialize() {
        this.api.getData(this.importantParameter);
    }

    @Input()
    public importantParameter: string;
}

The changes object also gives your access to the current and previous value of the object.

For ngDoCheck, this answer offers a great explanation: https://stackoverflow.com/a/42807309/9628950

Upvotes: 1

Related Questions