Bird
Raised Fist0
Angularframework~10 mins

Migrating from observables to signals in Angular - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a signal from an observable using Angular's toSignal function.

Angular
const countSignal = [1];
Drag options to blanks, or click blank then click option'
Asubscribe(count$)
Bfrom(count$)
Csignal(count$)
DtoSignal(count$)
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'from' which creates an observable from other sources.
Using 'signal' directly on an observable which is incorrect.
Trying to subscribe directly without conversion.
2fill in blank
medium

Complete the code to update the signal when the observable emits new values.

Angular
const countSignal = toSignal(count$[1] { initialValue: 0 });
Drag options to blanks, or click blank then click option'
A,
B.subscribe()
C.pipe()
D()
Attempts:
3 left
💡 Hint
Common Mistakes
Using dot notation instead of comma to separate arguments.
Trying to call subscribe inside toSignal.
Using parentheses incorrectly.
3fill in blank
hard

Fix the error in the code to correctly convert an observable to a signal with an initial value.

Angular
const userSignal = toSignal(user$[1] { initialValue: null });
Drag options to blanks, or click blank then click option'
A.pipe(
B()
C,
D.subscribe(
Attempts:
3 left
💡 Hint
Common Mistakes
Using dot notation or parentheses instead of a comma.
Trying to subscribe manually inside toSignal.
4fill in blank
hard

Complete the code to create a signal from an observable and provide an initial value.

Angular
const dataSignal = toSignal([1], { initialValue: [] });
Drag options to blanks, or click blank then click option'
Adata$
B,
C.pipe()
D()
Attempts:
3 left
💡 Hint
Common Mistakes
Using dot notation instead of comma.
Passing the options object as first argument.
5fill in blank
hard

Fill both blanks to convert an observable to a signal, set an initial value, and access the signal's current value.

Angular
const messageSignal = toSignal([1], { initialValue: 'Loading...' });
const currentMessage = messageSignal.[2];
Drag options to blanks, or click blank then click option'
Amessage$
B,
Cvalue
D()
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to call the signal as a function to get the value.
Passing options incorrectly.
Using wrong variable names.

Practice

(1/5)
1. What is the main benefit of migrating from Observable to signal() in Angular?
easy
A. More complex code but better performance
B. Signals require manual subscription management
C. Simpler reactive data handling with automatic UI updates
D. Signals do not support reactive updates

Solution

  1. Step 1: Understand the purpose of signals

    Signals in Angular provide a simpler way to handle reactive data by automatically updating the UI when the data changes.
  2. Step 2: Compare with observables

    Observables require subscriptions and manual management, while signals reduce this complexity and improve performance.
  3. Final Answer:

    Simpler reactive data handling with automatic UI updates -> Option C
  4. Quick Check:

    Signals simplify reactivity = B [OK]
Hint: Signals auto-update UI, observables need subscriptions [OK]
Common Mistakes:
  • Thinking signals require manual subscriptions
  • Believing signals increase code complexity
  • Assuming signals do not update UI reactively
2. Which of the following is the correct way to create a signal in Angular?
easy
A. const count = createSignal(0);
B. const count = new Signal(0);
C. const count = observable(0);
D. const count = signal(0);

Solution

  1. Step 1: Recall Angular signal creation syntax

    Angular uses the signal() function to create reactive signals.
  2. Step 2: Check options for correct syntax

    Only const count = signal(0); matches Angular's official syntax.
  3. Final Answer:

    const count = signal(0); -> Option D
  4. Quick Check:

    Use signal() function = C [OK]
Hint: Use signal() function, not new or createSignal [OK]
Common Mistakes:
  • Using new Signal() instead of signal()
  • Confusing with createSignal() from other frameworks
  • Using observable() which is for RxJS
3. Given this Angular code migrating from observable to signal:
const count = signal(0);

function increment() {
  count.set(count() + 1);
}

increment();
increment();
console.log(count());

What will be logged to the console?
medium
A. 0
B. 2
C. undefined
D. 1

Solution

  1. Step 1: Understand signal initial value and increments

    The signal count starts at 0. Each call to increment() adds 1.
  2. Step 2: Calculate final value after two increments

    After two calls, count is 0 + 1 + 1 = 2.
  3. Final Answer:

    2 -> Option B
  4. Quick Check:

    0 + 2 increments = 2 [OK]
Hint: Signals hold value, use .set() and () to get value [OK]
Common Mistakes:
  • Forgetting to call count() to get value
  • Assuming count is immutable without .set()
  • Confusing initial value with updated value
4. What is wrong with this migration from observable to signal?
const count = signal(0);

count.subscribe(value => console.log(value));

count.set(5);
medium
A. Signals do not have a subscribe method
B. You must call count() instead of count.set()
C. Signals require new keyword to create
D. count.set() does not update the signal value

Solution

  1. Step 1: Check signal API differences from observables

    Signals do not have a subscribe method; that is an observable feature.
  2. Step 2: Identify correct way to react to signal changes

    To react to signals, use computed signals or effects, not subscribe.
  3. Final Answer:

    Signals do not have a subscribe method -> Option A
  4. Quick Check:

    Signals lack subscribe() = A [OK]
Hint: Signals use effects, not subscribe() [OK]
Common Mistakes:
  • Trying to subscribe to signals like observables
  • Confusing signal() with observable()
  • Using count() incorrectly as a setter
5. You have this observable-based Angular code:
count$ = new BehaviorSubject(0);

increment() {
  this.count$.next(this.count$.value + 1);
}

How would you migrate this to use signals correctly?
hard
A. const count = signal(0); function increment() { count.set(count() + 1); }
B. const count = signal(0); function increment() { count = count() + 1; }
C. const count = new signal(0); function increment() { count.set(count() + 1); }
D. const count = signal(0); function increment() { count.next(count() + 1); }

Solution

  1. Step 1: Replace BehaviorSubject with signal()

    Use signal(0) to create a reactive value starting at 0.
  2. Step 2: Update increment function to use .set()

    Use count.set(count() + 1) to update the signal's value correctly.
  3. Final Answer:

    const count = signal(0); function increment() { count.set(count() + 1); } -> Option A
  4. Quick Check:

    Use signal() and .set() to update = A [OK]
Hint: Use .set() to update signals, not assignment or next() [OK]
Common Mistakes:
  • Trying to assign directly to signal variable
  • Using new keyword with signal()
  • Calling next() on a signal