What is the difference between OnChanges and DoCheck in Angular 2?

前端 未结 4 1742
野性不改
野性不改 2020-12-13 12:35

Difference between them seems to be very confusing to me. What is difference between them and exactly when they are called

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-13 13:19

    First of all Kudos to the above answers. I am adding a use-case for both of the functions which I have faced personally.

    ngOnChanges() (OnChanges): to detect changes for variables passed by value

    ngDoCheck() (DoCheck) : to detect changes for variable passed by reference such as arrays, which are not detected by ngOnChanges() as the old value and the new value have the same reference

提交回复
热议问题