[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_INITIALIZERto 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() {
}
}
[Angular] Configure an Angular App at Runtime的更多相关文章
- [Angular] Configure an Angular App at Compile Time with the Angular CLI
Compile time configuration options allow you to provide different kind of settings based on the envi ...
- 002——Angular 目录结构分析、app.module.ts 详解、以及 Angular 中创建组件、组件 详解、 绑定数据
一.目录结构分析 二. app.module.ts.组件分析 1.app.module.ts 定义 AppModule,这个根模块会告诉 Angular 如何组装该应用. 目前,它只声明了 AppCo ...
- Angular 2 to Angular 4 with Angular Material UI Components
Download Source - 955.2 KB Content Part 1: Angular2 Setup in Visual Studio 2017, Basic CRUD applicat ...
- 使用Angular CLI生成 Angular 5项目
如果您正在使用angular, 但是没有好好利用angular cli的话, 那么可以看看本文. Angular CLI 官网: https://github.com/angular/angular- ...
- Angular白名单&&Angular拦截器 全局通用
//angular 白名单全局通用 app.config([ '$compileProvider', function ($compileProvider) { $compileProvider.aH ...
- Angular 1与 Angular 2之间的一些差别
现在在用ng1.5.8做一个项目,ng的优点和特性我就不用多说了,ng1在陆续更新到1.5/1.6后就没再推出新版本了,ng2已经面世测试很久了,如同很多系统和框架一样,每个大的版本更新都会有新特性加 ...
- AngularJs angular.injector、angular.module
angular.injector 创建一个injector对象, 调用injector对象的方法可用于获取服务以及依赖注入. 格式:angular.injector(modules); modules ...
- angular.js 的angular.copy 、 angular.extend 、 angular.merge
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Angular - - angular.injector、angular.module
angular.injector 创建一个injector对象, 调用injector对象的方法可用于获取服务以及依赖注入. 格式:angular.injector(modules); modules ...
随机推荐
- 简述vuex的数据传递流程
简述vuex的数据传递流程 当组件进行数据修改的时候我们需要调用dispatch来触发actions里面的方法.actions里面的每个方法中都会有一个commit方法,当方法执行的时候会通过comm ...
- [POJ3233]Matrix Power Series 分治+矩阵
本文为博主原创文章,欢迎转载,请注明出处 www.cnblogs.com/yangyaojia [POJ3233]Matrix Power Series 分治+矩阵 题目大意 A为n×n(n<= ...
- ssm框架下上传图片及其他信息
先引入这两个包: <dependency> <groupId>commons-fileupload</groupId> <artifactId>comm ...
- BCB使用线程删除目录中的图片
BCB新建线程DeleteImgThread类.其会默认继承Thread类,然后在Execute函数中编写代码, void __fastcall DeleteImgThread::Execute() ...
- node之版本号升级和管理
如今非常多人预计和我一样项目中已经開始应用起nodeJS,而伴随着项目的需求,对nodejs版本号也有着各种需求.好了直接进入主题,如今node版本号管理网上有非常多方式.这里说两种: 第一种modu ...
- 【Java】【Flume】Flume-NG启动过程源代码分析(一)
从bin/flume 这个shell脚本能够看到Flume的起始于org.apache.flume.node.Application类,这是flume的main函数所在. main方法首先会先解析sh ...
- [jzoj 5177] [NOIP2017提高组模拟6.28] TRAVEL 解题报告 (二分)
题目链接: https://jzoj.net/senior/#main/show/5177 题目: 题解: 首先选出的泡泡怪一定是连续的一段 L,R 然后 L 一定属于虫洞左边界中的某一个 R 也同样 ...
- Codeforces 659F Polycarp and Hay 并查集
链接 Codeforces 659F Polycarp and Hay 题意 一个矩阵,减小一些数字的大小使得构成一个连通块的和恰好等于k,要求连通块中至少保持一个不变 思路 将数值从小到大排序,按顺 ...
- sql server 更新满足条件的某一条记录
上图数据:SNum为”18004XXXXX000001K2GW 4000 L1C“,OffLineStation为“OP1010”的有两条数据,当where条件中为上述两者时会同时更新这两条数据,并不 ...
- 3DS MAX玩家必看!70个提高渲染速度的小技巧
3DS MAX玩家必看!70个提高渲染速度的小技巧 (注:节省RAM不一定会加快渲染速度.请同学们根据实际情况加以利用.) 1. 尽量限制Ploygon数量,越少渲染速度越快 2. 如果使用Vray, ...