Bird
0
0

Why does this Angular component code cause a runtime error?

medium📝 Debug Q7 of 15
Angular - Signals
Why does this Angular component code cause a runtime error?
export class DemoComponent {
  @Input() count = 0;
  countSignal = signal(this.count);
}
AInput properties are not initialized when signals are created
Bsignal() cannot accept numbers as initial values
CcountSignal should be decorated with @Input()
DSignals must be declared inside constructor
Step-by-Step Solution
Solution:
  1. Step 1: Understand input initialization timing

    @Input() properties are set after component construction, so this.count is 0 initially but may change later.
  2. Step 2: Problem with signal initialization

    Initializing countSignal with this.count captures only initial value, not updates from parent.
  3. Final Answer:

    Input properties are not initialized when signals are created -> Option A
  4. Quick Check:

    Input values arrive after construction, signals need update logic [OK]
Quick Trick: Input values arrive after construction; signals need update logic [OK]
Common Mistakes:
  • Assuming input properties are ready during property initialization
  • Thinking signals cannot hold numbers
  • Believing signals must be in constructor

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes