Bird
0
0

Identify the error in this Angular component code that tries to manage state without NgRx:

medium📝 Debug Q6 of 15
Angular - State Management
Identify the error in this Angular component code that tries to manage state without NgRx:
export class SampleComponent {
  count = 0;
  increment() {
    count += 1;
  }
}
Acount must be a BehaviorSubject
Bcount should be declared inside increment method
CMissing 'this.' before count in increment method
Dincrement method should return count
Step-by-Step Solution
Solution:
  1. Step 1: Check variable reference inside method

    Inside class methods, properties must be accessed with 'this.'.
  2. Step 2: Identify missing 'this.'

    Code uses 'count += 1;' instead of 'this.count += 1;'.
  3. Final Answer:

    Missing 'this.' before count in increment method -> Option C
  4. Quick Check:

    Use 'this.count' to access class property [OK]
Quick Trick: Use 'this.' to access class properties inside methods [OK]
Common Mistakes:
  • Forgetting 'this.' causes runtime error
  • Declaring count inside method loses state
  • Thinking increment must return value

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes