Bird
0
0

Identify the issue in this Angular signal code:

medium📝 Debug Q7 of 15
Angular - State Management
Identify the issue in this Angular signal code:
const counter = signal(0);
counter.set(counter + 1);
console.log(counter());
ASignals cannot hold numeric values
Bsignal cannot be updated using set()
Ccounter() should not be called inside set()
Dcounter + 1 is invalid; should call counter() to get value
Step-by-Step Solution
Solution:
  1. Step 1: Understand signal value access

    Signals are functions; to get their value, call them as counter().
  2. Step 2: Analyze the set() argument

    counter + 1 tries to add 1 to the signal function itself, which is invalid.
  3. Step 3: Correct usage

    Use counter.set(counter() + 1) to increment the value.
  4. Final Answer:

    counter + 1 is invalid; should call counter() to get value -> Option D
  5. Quick Check:

    Always call signal() to get current value before updating [OK]
Quick Trick: Call signal() to get value before updating with set() [OK]
Common Mistakes:
  • Using signal variable directly instead of calling it
  • Misunderstanding set() usage
  • Assuming signals cannot hold numbers

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes