I have a component with a sub component timeline
.
<app-timeline [(editing)]="editingDate"></app-timeline>
In the timeline component I have these properties:
@Input() editing: boolean; // <--- detect change on this
@Output() editingChange = new EventEmitter();
How can I check when a change to the editing
property occurs from the timeline component? I need to emit the editing value whenever it changes.
Should I use a setter for the editing property then emit from there?
private _editing
set editing() { ... // emit }
Or is there another way?
The ngOnChanges can be used exactly for this
First make sure your component implements the ngOnChanges like this
export class TimelineComponent implements OnChanges
Then implement the ngOnChanges method like this
ngOnChanges(changes: SimpleChanges) {
if (changes.editing) {
console.log(changes.editing.currentValue);
}
}
Any time any input was changed it will enter the ngOnChanges method. This is also why the if statement was added since if any other input was changed the editing
property won't exist on the changes
object.
For primitive values it will trigger on any change. For reference values you need to change the actual reference, just changing a property of the object you have in the input won't work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With