Component Communication in Angular
Component Communication in Angular: Passing Data with input(), Sending Messages with output(), Component Lifecycle.
Updated
- Passing Data with input() (Apprentice): A component declares what it can be given with input(), or input.required() for what it must be given. A parent passes the data with a property binding on the component's tag, and the child reads it like a signal.
- Sending Messages with output() (Journeyman): A child component declares the messages it can send with output(), and sends one with emit. The parent listens with an event binding on the child's tag, and $event is the value that was sent, so the parent decides what happens.
- Component Lifecycle (Apprentice): A component is created, shown, updated and finally destroyed. ngOnInit runs once when it's ready, with its inputs set; ngOnDestroy runs when it's removed, to clean up timers and the like. effect runs code whenever the signals it reads change.