Angular có injector hierarchy: root/environment injector, route injector và element/component injectors.
Cleanup theo đúng scope bằng DestroyRef:
typescript
@Injectable()
export class RouteScopedStream {
private destroyRef = inject(DestroyRef)
start() {
interval(1000).pipe(takeUntilDestroyed(this.destroyRef)).subscribe()
}
}Provider ở scope càng thấp thì càng nhiều instance được tạo và bị destroy theo lifecycle của scope đó.
Đây là lợi thế để cô lập feature state, nhưng nếu service giữ subscription/timer/WebSocket mà không cleanup thì leak theo route hoặc component.