Component Communication

Apps are made of many small components that work together. You will pass data into a component with input(), send messages out with output(), and run code at the right moment in a component's life.

Updated

Component Communication in Angular

  1. 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.
  2. 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.
  3. 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.

Exercise: Product Reviews

  1. Exercise: Product Reviews (Journeyman): Build a product's reviews page from four components that talk through inputs and outputs. A star rating that shows or chooses a rating, a list of reviews, a form that sends a new review up, and the page that holds the data and puts them together.