|
| 1 | +# Environment configuration |
| 2 | + |
| 3 | +## Configuration strategies |
| 4 | + |
| 5 | +Angular supports two main configuration strategies: |
| 6 | + |
| 7 | +- **Build-time configuration** using environment files |
| 8 | +- **Runtime configuration** by loading values at application startup |
| 9 | + |
| 10 | +Choose the approach based on your deployment requirements. |
| 11 | + |
| 12 | +--- |
| 13 | + |
| 14 | +## Build-time configuration |
| 15 | + |
| 16 | +Environment files define configuration values that are replaced at build time. |
| 17 | + |
| 18 | +Generate environment files using the CLI: |
| 19 | + |
| 20 | +```bash |
| 21 | +ng generate environments |
| 22 | +``` |
| 23 | + |
| 24 | +This creates environment-specific files such as: |
| 25 | + |
| 26 | +```ts |
| 27 | +// environment.ts (production) |
| 28 | +export const environment = { |
| 29 | + production: true, |
| 30 | + apiUrl: 'https://api.example.com', |
| 31 | +}; |
| 32 | +``` |
| 33 | + |
| 34 | +```ts |
| 35 | +// environment.development.ts |
| 36 | +export const environment = { |
| 37 | + production: false, |
| 38 | + apiUrl: 'http://localhost:3000', |
| 39 | +}; |
| 40 | +``` |
| 41 | + |
| 42 | +Import the environment where needed: |
| 43 | + |
| 44 | +```ts |
| 45 | +import { environment } from '../environments/environment'; |
| 46 | + |
| 47 | +const apiUrl = environment.apiUrl; |
| 48 | +``` |
| 49 | + |
| 50 | +The Angular CLI replaces the appropriate file based on the build configuration. |
| 51 | + |
| 52 | +> Changes to environment files require rebuilding the application. |
| 53 | +
|
| 54 | +--- |
| 55 | + |
| 56 | +## Runtime configuration (advanced) |
| 57 | + |
| 58 | +In some scenarios, applications need to load configuration at runtime instead of build time. |
| 59 | + |
| 60 | +This allows the same build artifact to be deployed across multiple environments without rebuilding. |
| 61 | + |
| 62 | +A common approach is to load a JSON configuration file from the `assets` folder during application initialization. |
| 63 | + |
| 64 | +### Example |
| 65 | + |
| 66 | +```json |
| 67 | +// src/assets/config.json |
| 68 | +{ |
| 69 | + "apiUrl": "https://api.example.com" |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +Load the configuration before the application starts: |
| 74 | + |
| 75 | +```ts |
| 76 | +import { Injectable } from '@angular/core'; |
| 77 | +import { HttpClient } from '@angular/common/http'; |
| 78 | +import { firstValueFrom } from 'rxjs'; |
| 79 | + |
| 80 | +@Injectable({ providedIn: 'root' }) |
| 81 | +export class AppConfigService { |
| 82 | + private config!: { apiUrl: string }; |
| 83 | + |
| 84 | + constructor(private http: HttpClient) {} |
| 85 | + |
| 86 | + loadConfig(): Promise<void> { |
| 87 | + return firstValueFrom( |
| 88 | + this.http.get<{ apiUrl: string }>('/assets/config.json') |
| 89 | + ).then(cfg => { |
| 90 | + this.config = cfg; |
| 91 | + }); |
| 92 | + } |
| 93 | + |
| 94 | + get apiUrl(): string { |
| 95 | + return this.config.apiUrl; |
| 96 | + } |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +Register the loader during application bootstrap: |
| 101 | + |
| 102 | +```ts |
| 103 | +import { provideAppInitializer, inject } from '@angular/core'; |
| 104 | + |
| 105 | +provideAppInitializer(() => { |
| 106 | + const config = inject(AppConfigService); |
| 107 | + return config.loadConfig(); |
| 108 | +}); |
| 109 | +``` |
| 110 | + |
| 111 | +This ensures configuration is available before the application renders. |
| 112 | + |
| 113 | +> Runtime configuration is an advanced pattern and is not required for most applications. |
| 114 | +
|
| 115 | +--- |
| 116 | + |
| 117 | +## Choosing a strategy |
| 118 | + |
| 119 | +| Criteria | Build-time | Runtime | |
| 120 | +|----------|-----------|---------| |
| 121 | +| Change without rebuild | No | Yes | |
| 122 | +| Startup performance | Faster | Slight delay | |
| 123 | +| Complexity | Low | Moderate | |
| 124 | +| Deployment flexibility | Limited | High | |
| 125 | + |
| 126 | +Use build-time configuration for most applications, and runtime configuration when you need to deploy the same build across multiple environments. |
0 commit comments