|
| 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 | +> **Security note:** Environment files are bundled into the client-side application. |
| 19 | +> They are visible to anyone who can load the page. |
| 20 | +> Never store sensitive information like API keys, secrets, or credentials in environment files. |
| 21 | +> These values can be easily accessed by users. |
| 22 | +
|
| 23 | +Generate environment files using the CLI: |
| 24 | + |
| 25 | +```bash |
| 26 | +ng generate environments |
| 27 | +``` |
| 28 | + |
| 29 | +This creates environment-specific files such as: |
| 30 | + |
| 31 | +```ts |
| 32 | +// environment.ts (production) |
| 33 | +export const environment = { |
| 34 | + production: true, |
| 35 | + apiUrl: 'https://api.example.com', |
| 36 | +}; |
| 37 | +``` |
| 38 | + |
| 39 | +```ts |
| 40 | +// environment.development.ts |
| 41 | +export const environment = { |
| 42 | + production: false, |
| 43 | + apiUrl: 'http://localhost:3000', |
| 44 | +}; |
| 45 | +``` |
| 46 | + |
| 47 | +Import the environment where needed: |
| 48 | + |
| 49 | +```ts |
| 50 | +import { environment } from '../environments/environment'; |
| 51 | + |
| 52 | +const apiUrl = environment.apiUrl; |
| 53 | +``` |
| 54 | + |
| 55 | +The Angular CLI replaces the appropriate file based on the build configuration. |
| 56 | + |
| 57 | +> Changes to environment files require rebuilding the application. |
| 58 | +
|
| 59 | +--- |
| 60 | + |
| 61 | +## Runtime configuration (advanced) |
| 62 | + |
| 63 | +In some scenarios, applications need to load configuration at runtime instead of build time. |
| 64 | + |
| 65 | +This allows the same build artifact to be deployed across multiple environments without rebuilding. |
| 66 | + |
| 67 | +A common approach is to load a JSON configuration file from the `assets` folder during application |
| 68 | +initialization. |
| 69 | + |
| 70 | +### Example |
| 71 | + |
| 72 | +```json |
| 73 | +// src/assets/config.json |
| 74 | +{ |
| 75 | + "apiUrl": "https://api.example.com" |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +Load the configuration before the application starts: |
| 80 | + |
| 81 | +```ts |
| 82 | +import { Injectable } from '@angular/core'; |
| 83 | +import { HttpClient } from '@angular/common/http'; |
| 84 | + |
| 85 | +@Injectable({ providedIn: 'root' }) |
| 86 | +export class AppConfigService { |
| 87 | + private config!: { apiUrl: string }; |
| 88 | + |
| 89 | + private readonly http = inject(HttpClient); |
| 90 | + |
| 91 | + loadConfig() { |
| 92 | + return this.http.get<AppConfig>('/assets/config.json').pipe( |
| 93 | + tap(data => { |
| 94 | + this.config = data; |
| 95 | + }) |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + get apiUrl(): string { |
| 100 | + return this.config.apiUrl; |
| 101 | + } |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +Register the loader during application bootstrap: |
| 106 | + |
| 107 | +```ts |
| 108 | +import { provideAppInitializer, inject } from '@angular/core'; |
| 109 | + |
| 110 | +provideAppInitializer(() => { |
| 111 | + const config = inject(AppConfigService); |
| 112 | + return config.loadConfig(); |
| 113 | +}); |
| 114 | +``` |
| 115 | + |
| 116 | +This ensures configuration is available before the application renders. |
| 117 | + |
| 118 | +> Runtime configuration is an advanced pattern and is not required for most applications. |
| 119 | +
|
| 120 | +--- |
| 121 | + |
| 122 | +## Choosing a strategy |
| 123 | + |
| 124 | +| Criteria | Build-time | Runtime | |
| 125 | +|----------|-----------|---------| |
| 126 | +| Change without rebuild | No | Yes | |
| 127 | +| Startup performance | Faster | Slight delay | |
| 128 | +| Complexity | Low | Moderate | |
| 129 | +| Deployment flexibility | Limited | High | |
| 130 | + |
| 131 | +Use build-time configuration for most applications, and runtime configuration when you need to |
| 132 | +deploy the same build across multiple environments. |
0 commit comments