Loading...
Loading...
ALWAYS use when working with RxJS Observables, operators, and reactive patterns in Angular applications.
npx skill4agent add oguzhan18/angular-ecosystem-skills rxjstoSignaltoObservable@Component({
template: `
<div *ngIf="data$ | async as data">
{{ data.name }}
</div>
`
})
export class MyComponent {
data$ = this.service.getData();
}// switchMap - cancel previous, keep latest (search)
search(term: string): Observable<SearchResult[]> {
return this.searchService.search(term).pipe(
debounceTime(300),
distinctUntilChanged(),
switchMap(term => term ? this.http.get(...) : of([]))
);
}
// mergeMap - run concurrently (multiple API calls)
this.items$.pipe(
mergeMap(item => this.save(item))
);
// concatMap - run sequentially (order matters)
this.orders$.pipe(
concatMap(order => this.processOrder(order))
);
// exhaustMap - ignore while running (form submit)
this.submit$.pipe(
exhaustMap(() => this.submitForm())
);// Use takeUntil pattern
private destroy$ = new Subject<void>();
ngOnInit() {
this.data$.pipe(takeUntil(this.destroy$)).subscribe();
}
ngOnDestroy() {
this.destroy$.next();
this.destroy$.complete();
}
// Or use takeUntilDestroyed (Angular 16+)
private destroy$ = takeUntilDestroyed();catchErrorthis.http.get('/api/data').pipe(
catchError(error => {
console.error(error);
return of([]); // Return fallback value
})
);shareReplay(1)this.data$ = this.http.get('/api/data').pipe(
shareReplay(1)
);$// Convert Observable to Signal
users = toSignal(this.users$, { initialValue: [] });
// Convert Signal to Observable (if needed)
name$ = toObservable(this.name);