34 lines
883 B
Markdown
34 lines
883 B
Markdown
## Angular Services & Dependency Injection
|
|
|
|
### What is a Service?
|
|
A service is used to store reusable business logic that can be shared across multiple components.
|
|
Examples:
|
|
- Fetching data
|
|
- Authentication logic
|
|
- Utility functions
|
|
|
|
### Why Services are Needed?
|
|
- Avoids code duplication
|
|
- Improves maintainability
|
|
- Follows separation of concerns
|
|
|
|
### Creating a Service
|
|
ng generate service data
|
|
|
|
### Dependency Injection (DI)
|
|
Angular automatically provides services to components using DI.
|
|
constructor(private dataService: DataService) {}
|
|
|
|
### Using Service Methods
|
|
getMessage() {
|
|
return "Welcome to Angular Services";
|
|
}
|
|
|
|
### Practical Work Done
|
|
- Created services
|
|
- Injected services into components
|
|
- Shared data between components
|
|
- Tested service methods
|
|
|
|
### Learning Outcome
|
|
Understood how Angular manages shared logic efficiently using services and dependency injection. |