Why might this cause issues in a multi-component app?
medium
A. Because getData returns a number instead of an observable.
B. Because data is private and cannot be accessed directly.
C. Because setData does not return a value.
D. Because changes to data are not observable by components.
Solution
Step 1: Analyze state sharing method
The service stores data privately and exposes getter/setter but no observable pattern.
Step 2: Identify problem with state updates
Without observables, components won't react to changes automatically, causing stale views.
Final Answer:
Because changes to data are not observable by components. -> Option D
Quick Check:
Non-observable state = no automatic updates [OK]
Hint: 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
5. You are building a small Angular app with a few components sharing a simple counter state. You consider using NgRx but worry about complexity. Which approach best balances simplicity and shared state management?
hard
A. Use local variables in each component and synchronize manually with events.
B. Use a shared service with a BehaviorSubject to hold the counter and update it.
C. Keep the counter state only inside one component and pass it via inputs/outputs.
D. Implement full NgRx store with actions, reducers, and effects for the counter.
Solution
Step 1: Assess app complexity and state needs
Small app with simple shared counter needs easy shared state without heavy setup.
Step 2: Evaluate options for simplicity and sharing
Shared service with BehaviorSubject allows reactive updates and simple code, avoiding NgRx overhead.
Final Answer:
Use a shared service with a BehaviorSubject to hold the counter and update it. -> Option B
Quick Check:
Simple shared state = service + BehaviorSubject [OK]
Hint: Use BehaviorSubject service for simple shared state, avoid NgRx [OK]
Common Mistakes:
Choosing full NgRx for small apps
Using only inputs/outputs for shared state
Manually syncing local variables across components