Suggest an editImprove this articleRefine the answer for “What does the useFactory key do?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**`useFactory`** is a provider key that tells Angular to create the dependency using a factory function instead of a plain class or value. **Key point:** `useFactory` lets you compute a value dynamically and use other dependencies (`deps`) inside the factory.Shown above the full answer for quick recall.Answer (EN)ImageThe `useFactory` key tells Angular: > "Create the dependency using my function (factory), not just through a class or a value." --- ### Example: ```ts providers: [ { provide: 'API_URL', useFactory: () => { return window.location.hostname === 'localhost' ? 'http://localhost:3000' : 'https://api.example.com'; } } ] ``` Here Angular calls this function at startup and supplies its result (`API_URL`) as the dependency. --- ### When this is needed: - when the value needs to be **computed dynamically**; - when it depends on conditions (environment, settings, a token, and so on); - when the factory needs to use other dependencies internally. --- ### Example with a dependency: ```ts providers: [ { provide: ConfigService, useFactory: (apiUrl: string) => new ConfigService(apiUrl), deps: ['API_URL'] // dependencies for the factory } ] ``` --- **Summary:** `useFactory` is a way to tell Angular: *"get the dependency by calling my function, which decides on its own what to return."*For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.