When using @Input
in our components, we can implement ngOnChanges
to be notified whenever an input value has changed:
The above code works and would run whenever id
or name
are updated by the parent component. The problem is that this syntax can quickly become heavy when there are several @Input
s in your component.
Instead, we can do this:
Instead of using @Input()
on a class property, we can use it on an ES6 setter function. The benefit of the setter syntax is that it makes it obvious what kind of code we’re running when the @Input
value is set. No more ngOnChanges
needed.