Angular: In Lifecycle Hooks docs, it's not clear that ngOnChanges is not called when inputs are not used

Created on 27 Mar 2018  ·  1Comment  ·  Source: angular/angular

I'm submitting a...

[x] Documentation issue or request

Current behavior

Lifecycle Hooks page says this about ngOnChanges():

called before ngOnInit() and whenever one or more data-bound input properties change

and this about ngOnInit():

called [...] after the first ngOnChanges()

It's not clear from that description that ngOnChanges() will _not_ be called if the component is used _without_ providing any inputs.

Expected behavior

Documentation should be more clear about ngOnChanges() behavior: specifically, if a component has inputs declared but they are not used, then ngOnChanges() will _not_ be called before ngOnInit()

Minimal reproduction of the problem with instructions

https://stackblitz.com/edit/angular-ngonchanges

What is the motivation / use case for changing the behavior?

Judging from the of the docs, one may have an impression that ngOnChanges() will always be called first before ngOnInit() for a component with inputs, even if those inputs are not used by parent template/component.

This may lead to not-so-obvious accidental errors, such as removing ngOnInit() if it duplicates start-up logic of a component from ngOnChanges().

docs feature medium

Most helpful comment

This just caught me out as well.

The documentation really comes across like ngOnChanges is a requirement of ngOnInit firing, it also doesn't make it clear that ngOnChanges may not fire at all if no data is bound. When in reality, it's merely depicting the order they are called, not that they are related to each other being triggered or not.

Would be great to have this cleared up.

>All comments

This just caught me out as well.

The documentation really comes across like ngOnChanges is a requirement of ngOnInit firing, it also doesn't make it clear that ngOnChanges may not fire at all if no data is bound. When in reality, it's merely depicting the order they are called, not that they are related to each other being triggered or not.

Would be great to have this cleared up.

Was this page helpful?
0 / 5 - 0 ratings