InterviewHubPRO
Section Overview
Q131

Question 131 of 283

Part 6: Decorators, Dependency Injection & Tooling (124 - 150)

46%
Angular & Signals (283 Qs)•Part 6: Decorators, Dependency Injection & Tooling (124 - 150)

What is an RxJS Subject in Angular?

An RxJS Subject is a special type of Observable that allows values to be multicasted to many Observers. While plain Observables are unicast (each subscribed Observer owns an independent execution of the Observable), Subjects are multicast.

A Subject is like an Observable, but can multicast to many Observers. Subjects are like EventEmitters: they maintain a registry of many listeners.

typescript
import { Subject } from 'rxjs';

 const subject = new Subject<number>();

 subject.subscribe({
   next: (v) => console.log(`observerA: ${v}`)
 });
 subject.subscribe({
   next: (v) => console.log(`observerB: ${v}`)
 });

 subject.next(1);
 subject.next(2);