Services & Dependency Injection Flashcards
6 cards from real Angular Web Framework practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 6 Services & Dependency Injection flashcards as text
What is a service in Angular?
Answer: A class with a focused purpose that provides shared functionality across components
Angular services are TypeScript classes decorated with @Injectable() that encapsulate reusable business logic, data access, or utility functions that can be shared across multiple components.
What is dependency injection (DI) in Angular?
Answer: A design pattern where dependencies are provided to a class rather than created by it
Angular's DI system creates and provides instances of services to components and other services that declare dependencies, promoting loose coupling and testability.
What does 'providedIn: root' mean in an @Injectable() decorator?
Answer: The service is available application-wide as a singleton through the root injector
'providedIn: root' registers the service with the root injector, making a single instance available throughout the entire application and enabling tree-shaking of unused services.
What is the purpose of HttpClient in Angular?
Answer: To make HTTP requests to APIs and external services
HttpClient is Angular's built-in service for making HTTP requests (GET, POST, PUT, DELETE) to backend APIs, returning Observables that can be subscribed to for response handling.
What is an interceptor in Angular?
Answer: A service that intercepts and optionally modifies HTTP requests or responses globally
HTTP interceptors sit between the application and the HTTP client, allowing global modification of outgoing requests (adding auth tokens) or incoming responses (error handling).
What is the difference between a service and a component in Angular?
Answer: Components manage UI and views; services contain reusable business logic and data access
Components are responsible for presenting the user interface and handling user interactions, while services encapsulate reusable logic, data access, and cross-cutting concerns shared across components.