What is the difference between providedIn root and providing a service in a component?
Model answer
providedIn: 'root' registers one shared instance for the whole application, created when first needed. Providing a service in a component's providers array creates a new instance for that component and its children, and it is destroyed with the component.
Use root for shared state and API services. Use component-level providers when each instance needs its own state, such as one form helper per page or a dialog.
Learn this topic
Angular Tutorial: Beginner to Advanced .
Sign in to add your own answer.
More questions
- What is a directive? What are the types?
- What is the difference between ngClass and ngStyle, and how do they compare with class and style bindings?
- What is a pipe? Name some built-in pipes.
- How do you create a custom pipe? What is the difference between pure and impure pipes?
- What does the async pipe do, and why is it recommended?
- How do you build a custom attribute directive?