@@ -29,31 +29,31 @@ ng generate environments
2929This creates environment-specific files such as:
3030
3131``` ts
32- // environment.ts (production)
32+ // environment.ts
3333export const environment = {
34- production: true ,
3534 apiUrl: ' https://api.example.com' ,
3635};
3736```
3837
3938``` ts
4039// environment.development.ts
4140export const environment = {
42- production: false ,
4341 apiUrl: ' http://localhost:3000' ,
4442};
4543```
4644
4745Import the environment where needed:
4846
4947``` ts
50- import { environment } from ' ../environments/environment' ;
48+ import {environment } from ' ../environments/environment' ;
5149
5250const apiUrl = environment .apiUrl ;
5351```
5452
5553The Angular CLI replaces the appropriate file based on the build configuration.
5654
55+ If you need a development-mode check, use ` isDevMode() ` from ` @angular/core ` instead of relying on a manually maintained ` production ` flag.
56+
5757> Changes to environment files require rebuilding the application.
5858
5959---
@@ -79,20 +79,20 @@ initialization.
7979Load the configuration before the application starts:
8080
8181``` ts
82- import { Injectable } from ' @angular/core' ;
83- import { HttpClient } from ' @angular/common/http' ;
82+ import {Service , inject } from ' @angular/core' ;
83+ import {HttpClient } from ' @angular/common/http' ;
8484
85- @Injectable ({ providedIn: ' root ' } )
85+ @Service ( )
8686export class AppConfigService {
87- private config! : { apiUrl: string };
87+ private config! : {apiUrl: string };
8888
8989 private readonly http = inject (HttpClient );
9090
9191 loadConfig() {
9292 return this .http .get <AppConfig >(' /assets/config.json' ).pipe (
93- tap (data => {
93+ tap (( data ) => {
9494 this .config = data ;
95- })
95+ }),
9696 );
9797 }
9898
@@ -105,7 +105,7 @@ export class AppConfigService {
105105Register the loader during application bootstrap:
106106
107107``` ts
108- import { provideAppInitializer , inject } from ' @angular/core' ;
108+ import {provideAppInitializer , inject } from ' @angular/core' ;
109109
110110provideAppInitializer (() => {
111111 const config = inject (AppConfigService );
@@ -121,12 +121,12 @@ This ensures configuration is available before the application renders.
121121
122122## Choosing a strategy
123123
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 |
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 |
130130
131131Use build-time configuration for most applications, and runtime configuration when you need to
132132deploy the same build across multiple environments.
0 commit comments