Bird
0
0

You have this Angular service managing state without NgRx:

medium📝 Debug Q14 of 15
Angular - State Management
You have this Angular service managing state without NgRx:
export class SimpleService {
  private data = 0;

  setData(value: number) {
    this.data = value;
  }

  getData() {
    return this.data;
  }
}

Why might this cause issues in a multi-component app?
ABecause <code>getData</code> returns a number instead of an observable.
BBecause <code>data</code> is private and cannot be accessed directly.
CBecause <code>setData</code> does not return a value.
DBecause changes to <code>data</code> are not observable by components.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze state sharing method

    The service stores data privately and exposes getter/setter but no observable pattern.
  2. Step 2: Identify problem with state updates

    Without observables, components won't react to changes automatically, causing stale views.
  3. Final Answer:

    Because changes to data are not observable by components. -> Option D
  4. Quick Check:

    Non-observable state = no automatic updates [OK]
Quick Trick: State must be observable for components to update [OK]
Common Mistakes:
  • Thinking private variable blocks access
  • Believing return type causes update issues
  • Confusing method return with state reactivity

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes