Bird
0
0

What is the error in this Angular component code?

medium📝 Debug Q6 of 15
Angular - Signals
What is the error in this Angular component code?
export class SampleComponent {
  count = signal(0);
  increment() {
    this.count = this.count() + 1;
  }
}
ACannot assign number to signal property directly
Bsignal() function is used incorrectly
CMissing @Input() decorator
Dincrement method should be async
Step-by-Step Solution
Solution:
  1. Step 1: Analyze increment method

    The code tries to assign a number directly to the signal property, which is invalid.
  2. Step 2: Correct way to update signal

    Use this.count.set(newValue) to update the signal's value.
  3. Final Answer:

    Cannot assign number to signal property directly -> Option A
  4. Quick Check:

    Update signals with .set(), not direct assignment [OK]
Quick Trick: Update signals with .set(value), not direct assignment [OK]
Common Mistakes:
  • Assigning value directly to signal property
  • Forgetting to call .set() to update signal
  • Confusing signals with normal variables

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes