[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 ...
随机推荐
- Windows 10快速在指定目录打开命令行
一.我们在想要到达的目录上按住shift键并点击鼠标右键.看到了吗,这时候在弹出菜单里多了一个选项,就是"在此处打开命令窗口",我们点开看一下. 二.不过有时候我们需要以管理员的权 ...
- How-to-quick-getting-started-for-Frontend
一转眼已一年多不专注前端方面的开发工作,这一年前端方面的技术又是新天地,偶然接到内部团队邀请我给他们做一个前端的讲座,希望能帮助他们快速.且深刻了解前端这个行业以及行业内的知识,这可真有点让我为难,由 ...
- 如何在 Linux 上安装 Nginx (源码安装)
如何在 Linux( CentOS ) 上安装 Nginx 1.下载 nginx 链接 : https://pan.baidu.com/s/1sll0Hrf 密码 : xnem 2.安装 gcc ( ...
- CF16A Flag
CF16A Flag 题意翻译 题目描述 根据一项新的ISO标准,每一个国家的国旗应该是一个n×m的格子场,其中每个格子最多有10种不同的颜色.并且国旗应该有条纹:旗帜的每一行应包含相同颜色的方块,相 ...
- 【POJ 2485】 Highways
[POJ 2485] Highways 最小生成树模板 Prim #include using namespace std; int mp[501][501]; int dis[501]; bool ...
- MFC 加入背景图片并让控件背景透明
/*加入背景图片*/ BOOL CTOOLDlg::OnEraseBkgnd(CDC* pDC) { // TODO: 在此加入消息处理程序代码和/或调用默认值 CDialog::OnEraseB ...
- 50.AngularJs directive详解及示例代码
转自:https://www.cnblogs.com/best/tag/Angular/ 本教程使用AngularJs版本:1.5.3 AngularJs GitHub: https://github ...
- Python之Linux下的virtualenv
在使用 Python 开发的过程中,工程一多,难免会碰到不同的工程依赖不同版本的库的问题: 亦或者是在开发过程中不想让物理环境里充斥各种各样的库,引发未来的依赖灾难. 此时,我们需要对于不同的工程使用 ...
- Kali linux 2016.2(Rolling)中的Exploits模块详解
简单来将,这个Exploits模块,就是针对不同的已知漏洞的利用程序. root@kali:~# msfconsole Unable to handle kernel NULL pointer der ...
- 51nod 1649 齐头并进 (djikstra求最短路径,只用跑一次)
题目: 这道题有一个坑点:两种交通工具同时出发,中途不能停留在同一个小镇. 其实想通了就很简单,因为要么火车一步到达,要么汽车一步到达.不可能停留在同一个地方. 可是我还WA了好几次,蠢哭.想用BFS ...