Consider this Angular component using signals and operators. What will be the displayed value after the button is clicked twice?
import { Component, signal } from '@angular/core'; @Component({ selector: 'app-counter', standalone: true, template: ` <p>Count: {{ doubled() }}</p> <button (click)="increment()">Increment</button> ` }) export class CounterComponent { count = signal(0); doubled = this.count.map(x => x * 2); increment() { this.count.update(c => c + 1); } }
Remember the operator doubles the count signal value each time it changes.
The count signal starts at 0. Each click increments it by 1. After two clicks, count is 2. The doubled signal maps count to count * 2, so 2 * 2 = 4.
Which code snippet correctly creates a new signal that doubles the original signal's value using the map operator?
Angular signals have a map method to create derived signals.
In Angular signals, map is a method on the signal instance that returns a new signal with transformed values. The other options use RxJS or invalid syntax.
Given this code, why does it throw an error when the component renders?
import { Component, signal } from '@angular/core'; @Component({ selector: 'app-test', standalone: true, template: `{{ transformed() }}` }) export class TestComponent { base = signal(5); transformed = this.base.map(x => { if (x > 3) throw new Error('Value too high'); return x * 2; }); }
Think about when the map function executes and what happens if it throws.
The map function runs immediately when the signal value changes. If it throws an error, the component rendering fails with that error.
Given this Angular component code, what is the final value of result() after calling updateValues()?
import { Component, signal } from '@angular/core'; @Component({ selector: 'app-transform', standalone: true, template: `Result: {{ result() }}` }) export class TransformComponent { base = signal(1); result = this.base.map(x => x + 3); updateValues() { this.base.set(2); this.base.update(x => x * 5); } }
Remember that update uses the current signal value after set.
After set(2), base is 2. Then update(x => x * 5) changes base to 10. The result signal maps base + 3, so 10 + 3 = 13.
Which statement best explains why operators such as map are used to transform data streams in Angular signals?
Think about how reactive programming works with signals and derived values.
Operators like map create new signals derived from existing ones. These derived signals update automatically when the original changes, supporting reactive programming.