ngOnChanges(): "A lifecycle hook that is called when any data-bound property of a directive changes. Define an ngOnChanges() method to handle the ...
For further actions, you may consider blocking this person and/or reporting abuse
I have always used this approach to detect a specific change:
IMO it is simpler and more readable than the for/switch approach
I currently go with the following. I think it's solid, and well readable:
defaultWidth
anddefaultHeight
are some imported globally controlled variables.need to add
let change = changes[propName];
otherwisechange.currentValue
is undefinedIn this case, isn't it better to use a setter for each @Input instead of listen to ngOnChanges?
I think that comes down to personal preference. Personally, I find Input getters and setters to be too verbose. I like all inputs handled in the same place.
checkout subjectize, which wraps the setter under the hood and you don't need a private variable.
I was practicing onChanges and ran into your post. Although I have only one input in my training app, I did fix it and added SimpleChanges to take care of the next developer, which happens to be me :)
I general life though from my observation there are few people who actually care about the next developer. Probably 2% from every 100 developers. It's sad, but everybody is obsessed to deliver smth. working, so people write some no so clean code. They get rewarded for such quick delivery, while nobody cares that the next developer will have a tough life...
this is a great way to illustrate how ngOnChanges works, thanks.
as pointed out by others, mistake in line:
WTF is 'change' in your example? I get cannot find name 'change'.
It's an oversight for sure! Use Changes[propName] instead
wt ?? it would be nice to put html in here..
Thanks man it was great ,I newly used ngOnChanges() , and by seeing your post I avoided future headaches !
I think that '' if (changes.hasOwnProperty(propName)) '' is useless
better to use '' if (changes[propName].currentValue) ''
Thanks Nick! Very helpful article.