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. Each button click calls increment() which updates the signal by adding 1. After two clicks, the count becomes 2 and the template reflects this.
fullName() after the following code runs?import { signal, computed } from '@angular/core'; const firstName = signal('Jane'); const lastName = signal('Doe'); const fullName = computed(() => `${firstName()} ${lastName()}`); firstName.set('John');
The firstName signal is updated to 'John'. The fullName computed signal reacts to this change and returns 'John Doe'.
Option C tries to assign a number directly to the signal variable count, which is not allowed. Signals must be updated using set() or update() methods.
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 tries to display count directly, but signals are functions and must be called with parentheses like count() to get their value. Without calling, Angular does not track changes.
counter() after running this code snippet?import { signal } from '@angular/core'; const counter = signal(0); counter.update(c => { counter.update(c2 => c2 + 1); return c + 1; });
The outer update reads the current value 0 as c. Inside the callback, the inner update reads the current value (still 0), computes 1, and sets the signal to 1. The outer callback then returns c + 1 = 1, and the outer update sets the signal to 1. Final value is 1.