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的更多相关文章

  1. [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 ...

  2. 002——Angular 目录结构分析、app.module.ts 详解、以及 Angular 中创建组件、组件 详解、 绑定数据

    一.目录结构分析 二. app.module.ts.组件分析 1.app.module.ts 定义 AppModule,这个根模块会告诉 Angular 如何组装该应用. 目前,它只声明了 AppCo ...

  3. 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 ...

  4. 使用Angular CLI生成 Angular 5项目

    如果您正在使用angular, 但是没有好好利用angular cli的话, 那么可以看看本文. Angular CLI 官网: https://github.com/angular/angular- ...

  5. Angular白名单&&Angular拦截器 全局通用

    //angular 白名单全局通用 app.config([ '$compileProvider', function ($compileProvider) { $compileProvider.aH ...

  6. Angular 1与 Angular 2之间的一些差别

    现在在用ng1.5.8做一个项目,ng的优点和特性我就不用多说了,ng1在陆续更新到1.5/1.6后就没再推出新版本了,ng2已经面世测试很久了,如同很多系统和框架一样,每个大的版本更新都会有新特性加 ...

  7. AngularJs angular.injector、angular.module

    angular.injector 创建一个injector对象, 调用injector对象的方法可用于获取服务以及依赖注入. 格式:angular.injector(modules); modules ...

  8. angular.js 的angular.copy 、 angular.extend 、 angular.merge

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. Angular - - angular.injector、angular.module

    angular.injector 创建一个injector对象, 调用injector对象的方法可用于获取服务以及依赖注入. 格式:angular.injector(modules); modules ...

随机推荐

  1. Python学习笔记(5)practice:shopping_cart

    2019-02-27 原代码: money = int(input("请输入金额:")) list = ["phone", "clothes" ...

  2. React 中的 AJAX 请求:获取数据的方法

    React 中的 AJAX 请求:获取数据的方法 React 只是使用 props 和 state 两处的数据进行组件渲染. 因此,想要使用来自服务端的数据,必须将数据放入组件的 props 或 st ...

  3. js兼容性——获取当前浏览器窗口的宽高

    通过onresize事件 window.onresize = function () { document.title = client().width + " "+ client ...

  4. javascript-js常用插件集合

    area.js 中国地区分级的js代码  Scripts/crypto.js  CryptoJS (crypto.js) 为 JavaScript 提供了各种各样的加密算法               ...

  5. java解析XML saxReader.read(xml) 错误:org.dom4j.DocumentException: no protocol

    java解析XML saxReader.read(xml) 错误:org.dom4j.DocumentException: no protocol 完整错误信息: org.dom4j.Document ...

  6. 怎样用批处理来执行多个exe文件

    怎样用批处理来运行多个exe文件 @echo off start *****.exe start *****.exe start *****.exe start *****.exe 接着我们就能够运行 ...

  7. Android 开发之集成百度地图的定位与地图展示

    app 应用中,大多数应用都具有定位功能,百度定位就成了开发人员的集成定位功能的首选,近期也在做定位功能,可是发现百度真是个大坑啊, sdk 命名更新了,相关代码却不更新,害得我花费了非常长时间来研究 ...

  8. Spring整合Shiro从源代码探究机制

    首先从例如以下配置開始说起 ShiroDbFilterFactoryBean继承了ShiroFilterFactoryBean这个由jar提供的bean类, 而且它实现了InitializingBea ...

  9. jquery动态表格,动态添加表格行

    转载收藏于:https://www.cnblogs.com/zhangqs008/archive/2013/05/09/3618459.html 效果图:   Html:<html> &l ...

  10. maven/ssm框架搭建

    好久没有写java了,昨天学了下maven,不用手动的下载和添加jar包,实在是太方便. ------------------------------------------------------- ...