d48cda4081
CI / Test (ubuntu-latest, Node 18.x, bun) (push) Failing after 15m1s
CI / Test (ubuntu-latest, Node 18.x, npm) (push) Failing after 15m1s
CI / Test (ubuntu-latest, Node 18.x, pnpm) (push) Failing after 15m1s
CI / Test (ubuntu-latest, Node 18.x, yarn) (push) Failing after 15m1s
CI / Test (ubuntu-latest, Node 20.x, bun) (push) Failing after 17m13s
CI / Test (ubuntu-latest, Node 20.x, npm) (push) Failing after 18m42s
CI / Test (ubuntu-latest, Node 20.x, pnpm) (push) Failing after 15m0s
CI / Test (ubuntu-latest, Node 20.x, yarn) (push) Failing after 49m44s
CI / Test (ubuntu-latest, Node 22.x, bun) (push) Failing after 51m55s
CI / Test (ubuntu-latest, Node 22.x, pnpm) (push) Failing after 21m57s
CI / Test (ubuntu-latest, Node 22.x, npm) (push) Failing after 37m39s
CI / Test (ubuntu-latest, Node 22.x, yarn) (push) Failing after 34m7s
CI / Validate Components (push) Failing after 37m15s
CI / Python Tests (push) Failing after 10m1s
CI / Security Scan (push) Failing after 10m1s
CI / Lint (push) Failing after 17m12s
CI / Coverage (push) Failing after 20m19s
CI / Test (macos-latest, Node 18.x, bun) (push) Has been cancelled
CI / Test (macos-latest, Node 18.x, npm) (push) Has been cancelled
CI / Test (macos-latest, Node 18.x, pnpm) (push) Has been cancelled
CI / Test (macos-latest, Node 18.x, yarn) (push) Has been cancelled
CI / Test (windows-latest, Node 18.x, npm) (push) Has been cancelled
CI / Test (windows-latest, Node 18.x, pnpm) (push) Has been cancelled
CI / Test (windows-latest, Node 18.x, yarn) (push) Has been cancelled
CI / Test (macos-latest, Node 20.x, bun) (push) Has been cancelled
CI / Test (macos-latest, Node 20.x, npm) (push) Has been cancelled
CI / Test (macos-latest, Node 20.x, pnpm) (push) Has been cancelled
CI / Test (macos-latest, Node 20.x, yarn) (push) Has been cancelled
CI / Test (windows-latest, Node 20.x, npm) (push) Has been cancelled
CI / Test (windows-latest, Node 20.x, pnpm) (push) Has been cancelled
CI / Test (windows-latest, Node 20.x, yarn) (push) Has been cancelled
CI / Test (macos-latest, Node 22.x, bun) (push) Has been cancelled
CI / Test (macos-latest, Node 22.x, npm) (push) Has been cancelled
CI / Test (macos-latest, Node 22.x, pnpm) (push) Has been cancelled
CI / Test (macos-latest, Node 22.x, yarn) (push) Has been cancelled
CI / Test (windows-latest, Node 22.x, npm) (push) Has been cancelled
CI / Test (windows-latest, Node 22.x, pnpm) (push) Has been cancelled
CI / Test (windows-latest, Node 22.x, yarn) (push) Has been cancelled
73 lines
2.2 KiB
Markdown
73 lines
2.2 KiB
Markdown
# Defining Dependency Providers
|
|
|
|
Angular offers automatic and manual ways to provide dependencies to its Dependency Injection (DI) system.
|
|
|
|
## Automatic Provision
|
|
|
|
The most common way to provide a service is using `providedIn: 'root'` on an `@Injectable()`.
|
|
|
|
### InjectionToken
|
|
|
|
Use `InjectionToken` for non-class dependencies (configuration objects, functions, primitives). An `InjectionToken` can also be automatically provided.
|
|
|
|
```ts
|
|
import {InjectionToken} from '@angular/core';
|
|
|
|
export interface AppConfig {
|
|
apiUrl: string;
|
|
}
|
|
|
|
export const APP_CONFIG = new InjectionToken<AppConfig>('app.config', {
|
|
providedIn: 'root',
|
|
factory: () => ({apiUrl: 'https://api.example.com'}),
|
|
});
|
|
```
|
|
|
|
## Manual Provision
|
|
|
|
You use the `providers` array when a service lacks `providedIn`, when you want a new instance for a specific component, or when configuring runtime values.
|
|
|
|
```ts
|
|
@Component({
|
|
providers: [
|
|
// Shorthand for { provide: LocalService, useClass: LocalService }
|
|
LocalService,
|
|
|
|
// useClass: Swap implementations
|
|
{provide: Logger, useClass: BetterLogger},
|
|
|
|
// useValue: Provide static values
|
|
{provide: API_URL_TOKEN, useValue: 'https://api.example.com'},
|
|
|
|
// useFactory: Generate value dynamically
|
|
{
|
|
provide: ApiClient,
|
|
useFactory: (http = inject(HttpClient)) => new ApiClient(http),
|
|
},
|
|
|
|
// useExisting: Create an alias
|
|
{provide: OldLogger, useExisting: NewLogger},
|
|
|
|
// multi: Provide multiple values for the same token as an array
|
|
{provide: INTERCEPTOR_TOKEN, useClass: AuthInterceptor, multi: true},
|
|
],
|
|
})
|
|
export class Example {}
|
|
```
|
|
|
|
## Scopes of Providers
|
|
|
|
- **Application Bootstrap**: Global singletons. Use for HTTP clients, logging, or app-wide config.
|
|
- **Component/Directive**: Isolated instances. Use for component-specific state or forms. Services are destroyed when the component is destroyed.
|
|
- **Route**: Feature-specific services loaded only with specific routes.
|
|
|
|
## Library Pattern: `provide*` functions
|
|
|
|
Library authors should export functions that return provider arrays to encapsulate configuration:
|
|
|
|
```ts
|
|
export function provideAnalytics(config: AnalyticsConfig): Provider[] {
|
|
return [{provide: ANALYTICS_CONFIG, useValue: config}, AnalyticsService];
|
|
}
|
|
```
|