Angular Services
Angular Services: Services and inject(), Why Share Data Between Components?, Creating a Shared Data Service.
Updated
- Services and inject() (Apprentice): A service is a class for work that isn't about showing anything, such as calculations or calling an API. Mark it @Service(), and any component gets the one shared instance with inject(), like dependency injection in ASP.NET Core.
- Why Share Data Between Components? (Journeyman): Inputs and outputs only connect parents and children. When components far apart need the same data, such as a cart shown in the header and filled from a product page, passing it through every component in between gets messy. A shared service holds it in one place instead.
- Creating a Shared Data Service (Journeyman): A shared data service keeps its state in a private signal, shows it to components read-only with asReadonly() and computed values, and changes it only through its own methods. Every component that injects it sees the same, up-to-date data.