angular-services
Installation
SKILL.md
Angular Services
Version: Angular 21 (2025) Tags: Services, @Injectable, DI
References: Services Guide • @Injectable API
Best Practices
- Create service with providedIn
@Injectable({ providedIn: 'root' })
export class DataService {
getData() {
return this.http.get('/api/data');
}
}
- Use inject() function
@Injectable({ providedIn: 'root' })
export class UserService {
private http = inject(HttpClient);
getUsers() {
return this.http.get<User[]>('/api/users');
}
}
- Use factory providers
@Injectable({
providedIn: 'root',
useFactory: () => new LoggerService(environment.production)
})
export class LoggerService {
constructor(private isProduction: boolean) {}
}
- Use providedIn: 'any' for lazy services
@Injectable({ providedIn: 'any' })
export class LazyService {}
- Use service in component
@Component({})
export class MyComponent {
private dataService = inject(DataService);
data$ = this.dataService.getData();
}
- Use multiple services
@Component({})
export class MyComponent {
private auth = inject(AuthService);
private http = inject(HttpClient);
private router = inject(Router);
}
- Use service for shared state
@Injectable({ providedIn: 'root' })
export class CartService {
private items = signal<Item[]>([]);
cartItems = this.items.asReadonly();
addItem(item: Item) {
this.items.update(items => [...items, item]);
}
}
Related skills
More from oguzhan18/angular-ecosystem-skills
angular-tailwind
ALWAYS use when working with Angular and Tailwind CSS, Tailwind configuration, utility-first CSS, or styling Angular applications with Tailwind.
139angular-animations
>-
137rxjs
ALWAYS use when working with RxJS Observables, operators, and reactive patterns in Angular applications.
135angular-material
ALWAYS use when working with Angular Material components, CDK, or Material Design in Angular applications.
131angular-security
ALWAYS use when working with Angular Security, XSS prevention, CSRF protection, Content Security Policy, or sanitization in Angular applications.
130angular-bootstrap
ALWAYS use when working with Angular Bootstrap, ng-bootstrap, Bootstrap components in Angular, or Bootstrap 5 integration.
129