[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() {
- }
- }
[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 ...
随机推荐
- input只能输入数字或两位小数
/** * [只能输入数字和两位小数] * 举例:<input type="text" onkeyup="num(this)" size="10 ...
- STM32 的 printf() 函数串口重定向(HAL库标准库都适用)
1.建立工程 2.核心:添加新文件usar_fputc.c (名字随便自己命名),把文件添加到项目中去 #include "stdio.h" #include "stm3 ...
- js 对象的创建方式和对象的区别
js一个有三种方法创建对象,这里做一个总结. 1.对象直接量 所谓对象直接量,可以看做是一副映射表,这个方法也是最直接的一个方法,个人比较建议, 1 2 3 4 5 6 7 8 9 10 11 12 ...
- maven规定的目录
Maven规定的目录结构 若要使用Maven,那么项目的目录结构必须符合Maven的规范 ,如写一个使用Spring的Web项目就需要引入大量的jar包.一个项目Jar包的数量之多往往让我们瞠目结舌, ...
- Java ThreadLocal类学习
ThreadLocal为线程局部变量,通过线程名(key)-对象(value)的Map来获取每个线程对应的对象.我们不能通过ThreadLocal处理多线程并发问题,但是每个线程可以通过ThreadL ...
- atitit。流程图的设计与制作 attilax 总结
atitit.流程图的设计与制作 attilax 总结 1. 流程图的规范1 2. 画图语言2 2.1. atitit.CSDN-markdown编辑器2 2.2. js-sequence-diagr ...
- 根据数据表自动生成javaBean
package fanshe; import java.io.File; import java.io.FileWriter; import java.io.IOException; import j ...
- UVA 11971 - Polygon 数学概率
Polygon John has been given a segment of lenght N, however he n ...
- ElasticSearch 深入理解 三:集群部署设计
ElasticSearch 深入理解 三:集群部署设计 ElasticSearch从名字中也可以知道,它的Elastic跟Search是同等重要的,甚至以Elastic为主要导向. Elastic即可 ...
- [JZOJ 5465] [NOIP2017提高A组冲刺11.9] 道路重建 解题报告 (e-dcc+树的直径)
题目链接: http://172.16.0.132/senior/#main/show/5465 题目: 小X所居住的X国共有n个城市,有m条无向道路将其连接.作为一个统一的国家,X 城的任意两个城市 ...