asReadonly()

noun

say it: “AZ reed-OH-nee” /æz riːdˈoʊnli/

A signal method that returns a read-only view, so others can read the signal but not change it.

What is asReadonly() in Angular?

asReadonly() — A method on a writable signal that returns a read-only view of it. A service keeps the writable signal private and shares the read-only one, so components can read the data and react to it but only the service can change it.

@Service()
export class Cart {
  private readonly items = signal<string[]>([]);
  readonly contents = this.items.asReadonly();
  add(item: string): void { this.items.update(list => [...list, item]); }
}

In a sentence

  • “The service shares contents through asReadonly(), so only the service can add items.”

Level: Journeyman · More Angular terms at this level

Updated