Loading...
Loading...
ALWAYS use when working with Angular Reactive programming, BehaviorSubject, Observable patterns, or reactive state management in Angular.
npx skill4agent add oguzhan18/angular-ecosystem-skills angular-reactive@Injectable({ providedIn: 'root' })
export class StateService {
private state = new BehaviorSubject<State>(initialState);
state$ = this.state.asObservable();
updateState(newState: Partial<State>) {
this.state.next({ ...this.state.value, ...newState });
}
}@Injectable({ providedIn: 'root' })
export class DataService {
private http = inject(HttpClient);
getData(): Observable<Data[]> {
return this.http.get<Data[]>('/api/data');
}
}@Component({
template: `
@if (data$ | async; as data) {
{{ data.name }}
}
`
})
export class MyComponent {
data$ = this.service.getData();
}data$ = this.http.get('/api/data').pipe(
shareReplay(1)
);@Component({})
export class MyComponent implements OnDestroy {
private destroy$ = new Subject<void>();
ngOnInit() {
this.data$.pipe(takeUntil(this.destroy$)).subscribe();
}
ngOnDestroy() {
this.destroy$.next();
this.destroy$.complete();
}
}