import { Component, signal } from '@angular/core'; @Component({ selector: 'app-counter', standalone: true, template: `<button (click)="increment()">Increment</button><p>Count: {{ count() }}</p>` }) export class CounterComponent { count = signal(0); increment() { this.count.update(c => c + 1); } }
The signal count starts at 0. The template calls count() to get the current value. Clicking the button calls increment(), which updates the signal by adding 1. The displayed count updates reactively.
status() after the code runs?import { Component, signal } from '@angular/core'; @Component({ selector: 'app-status', standalone: true, template: `<p>Status: {{ status() }}</p>` }) export class StatusComponent { status = signal('idle'); constructor() { this.status.set('loading'); this.status.update(s => s + '...'); this.status.set('done'); } }
The signal status starts as 'idle'. It is set to 'loading', then updated to 'loading...', then finally set to 'done'. The last set call overwrites previous values, so the final value is 'done'.
set expects a value, not a signal function or signal itself.Option B tries to set the signal to count + 1, but count is a signal function, not a number. This causes a type error, which is a syntax misuse here.
import { Component, signal } from '@angular/core'; @Component({ selector: 'app-broken-counter', standalone: true, template: `<button (click)="increment()">Increment</button><p>Count: {{ count }}</p>` }) export class BrokenCounterComponent { count = signal(0); increment() { this.count.update(c => c + 1); } }
The template uses {{ count }} which prints the signal function itself, not its value. It should use {{ count() }} to get the current value and update reactively.
import { Component, signal, computed } from '@angular/core'; @Component({ selector: 'app-loop', standalone: true, template: `<p>Value: {{ value() }}</p>` }) export class LoopComponent { count = signal(0); value = computed(() => { if (this.count() < 3) { this.count.update(c => c + 1); } return this.count(); }); }
Updating a signal inside a computed signal causes the computed to re-run, which triggers another update, creating an infinite loop and runtime error.