Bird
0
0

Find the mistake in this Angular service using the Factory pattern:

medium📝 Debug Q7 of 15
Angular - Advanced Patterns
Find the mistake in this Angular service using the Factory pattern:
export function createService() { return new Service(); }
@Injectable({ providedIn: 'root', useFactory: createService })
export class Service {}
AFactory function returns an instance of Service before Service is declared
BuseFactory should be a string, not a function
CService class cannot be injectable
DprovidedIn cannot be 'root' with useFactory
Step-by-Step Solution
Solution:
  1. Step 1: Check order of declarations

    The factory function createService returns new Service(), but Service class is declared after it, causing a reference error.
  2. Step 2: Validate other options

    useFactory should be a string, not a function is wrong because useFactory expects a function. Service class cannot be injectable is false; services can be injectable. providedIn cannot be 'root' with useFactory is incorrect; providedIn: 'root' works with useFactory.
  3. Final Answer:

    Factory function returns an instance of Service before Service is declared -> Option A
  4. Quick Check:

    Factory function must be declared after class or use forward references [OK]
Quick Trick: Factory function must not reference class before declaration [OK]
Common Mistakes:
  • Using string instead of function for useFactory
  • Thinking services cannot be injectable
  • Believing providedIn 'root' conflicts with useFactory

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes