[Angular] Configure an Angular App at Runtime
It always again happens (especially in real world scenarios) that you need to configure your Angular app at runtime. That configuration might be fetched from some backend API, or it might be some simple JSON configuration sitting on your deployment server. The key here is that you want to be able to dynamically modify and adjust that configuration without the need to re-compile and re-deploy your application. Also, you most probably want that configuration to be loaded and ready once your application bootstrapping is done. In this lesson we learn how to make use of the APP_INITIALIZER
to hook into Angular's initialization process. That will allow us to inject some configuration into our app just when it is about to start up.
// app.module.ts const appInitializerFn = (configService: AppInitConfigService) => { return () => { return configService.loadAppConfig(); }; }; ... providers: [ AppInitConfigService, { provide: APP_INITIALIZER, useFactory: appInitializerFn, deps: [AppInitConfigService], multi: true } ],
// app.init-config.service.ts import {Injectable} from '@angular/core'; import {HttpClient} from '@angular/common/http'; @Injectable() export class AppInitConfigService { private appConfig; constructor(private http: HttpClient) { } getConfig() { return this.appConfig; } loadAppConfig() { return this.http.get('/assets/data/app.config.json') .toPromise() .then((config) => this.appConfig = config); } }
App will defer to load after the config is loaded.
We can display the config's content in the component:
import {ChangeDetectionStrategy, Component, OnInit} from '@angular/core'; import {Observable} from 'rxjs/Observable'; import {Store} from '@ngrx/store'; import 'rxjs/add/operator/do'; import 'rxjs/add/operator/shareReplay'; import 'rxjs/add/operator/map'; import * as fromRoot from '../../store/reducers/index'; import * as authActions from '../../../auth/actions/auth'; import {AuthService} from '../../../auth/services/auth.service'; import {AppInitConfigService} from '../../../app.init-config.service'; @Component({ selector: 'ld-app', changeDetection: ChangeDetectionStrategy.OnPush, templateUrl: './ld-app.component.html', styleUrls: ['./ld-app.component.scss'] }) export class LdAppComponent implements OnInit { config: any; constructor( private configService: AppInitConfigService) { this.config = this.configService.getConfig(); } ngOnInit() { } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2017-01-23 [Compose] Isomorphisms and round trip data transformations
2015-01-23 [Bootstrap] 1. container & container-fluid