liveBook · Manning

Read-only signal

As explained in Pro Angular 16

The asReadonly method creates a read-only view of a writable Angular signal.

A read-only signal is a public Signal view created from a private WritableSignal with asReadonly(). It exposes the current value and supports computed signals, while preventing callers from changing the value directly. section 6.2.1 section 10.4.4

Use the pattern to keep signal changes inside the class or service that owns the state:

Protecting cart state

private linesSignal = signal<CartLine[]>([]);\n\nget lines(): Signal<CartLine[]> {\n return this.linesSignal.asReadonly();\n}\n\naddLine(product: Product, quantity = 1) {\n this.linesSignal.mutate(lines => {\n // add the selection\n });\n}\n\nConsumers can read lines() or build computed signals from it, but must use Cart methods to add or remove selections. section 6.2.1

Listing 20.10. The contents of the message.service.ts file in the src/app/messages folder
import { Injectable, signal } from "@angular/core";
import { Message } from "./message.model";
 
@Injectable()
export class MessageService {
    private writableMessages = signal<Message[]>([]);
 
    messages = this.writableMessages.asReadonly();
 
    reportMessage(msg: Message) {
        this.writableMessages.mutate(msgs => msgs.push(msg));
    }
}

Shows a complete Angular service that stores messages in a private signal, exposes messages as read-only, and appends through reportMessage(). section 20.3.1

How Pro Angular 16 covers it

How deep it goes
Explains the private-writable/public-read-only pattern with runnable service and model examples, but does not descend into signal implementation internals.
Examples
Demonstrates cart lines, product repositories, user messages, and shared create-or-edit state exposed through read-only signals.
What it assumes
Assumes familiarity with Angular signals and basic classes or services, while demonstrating signal reading and mutation patterns in context.
What you can do after
Apply it via Angular signals to expose reactive state safely while routing changes through explicit model or service methods.
How it connects
Connects WritableSignal, Signal, computed signals, services, dependency injection, and set, update, or mutate operations.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro Angular 16. The section links point to the parts of the book it draws on.