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. Ubuntu下使用crontab部署定时任务

    Ubuntu下使用crontab部署定时任务 安装cron apt-get install cron 开启crontab日志 默认情况下的日志是没有开启的,我们需要找到 /etc/rsyslog.d/ ...

  2. php $_SERVER['PHP_SELF']安全漏洞

    REQUEST_URI 返回的是包括后面数据串的地址,如 index.php?str=1234 PHP_SELF 是 index.php ------------------------------- ...

  3. 2015 Multi-University Training Contest 4 hdu 5334 Virtual Participation

    Virtual Participation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Ot ...

  4. 彻底理解tomcat是怎样多线程处理http请求并将代码执行到controller里的的

    彻底理解tomcat是怎样多线程处理http请求并将代码执行到controller里的的 1.线程池,thread = threadPool.getThread(),thread.executeHtt ...

  5. POJ 2447

    挺水的一题.其实只要理解了RSA算法,就知道要使用大整数分解的方法来直接模拟了. 不过,要注意两个INT64的数相乘来超范围 #include <iostream> #include &l ...

  6. [ReactVR] Render Custom 3D Objects Using the Model Component in React VR

    React VR isn't limited to simple 3D primitives. By using the <Model/> Component we can place a ...

  7. iOS开发UI调试神器----Reveal

    做iOS的开发,UI是非常非常重要的一环.调试时我们一般用模拟器,提交前用真机做測试.用模拟器来调试UI效果尽管快捷方便,但有时仍然希望有更强大的工具来帮助分析UI,尤其是专注在UI的效果调试时.近期 ...

  8. Android简单实现BroadCastReceiver广播机制

    Android中广播的作用是很明显的,当我们收到一条信息,可能我们的应用须要处理一些数据.可能我们开机.我们的应用也须要处理一些数据,这里都用到了广播机制,这里简单的实现了一个自己定义广播.看实例: ...

  9. simple_strtoul()分析

    此函数有以下几点值得注意:1.第一个参数中的const.一般在函数的形参中,如果我们只是希望调用者使用该参数,而不会去改变该参数内容(一般是指针指向的内容),则可以声明为const.2.第二个参数.C ...

  10. oracle日常维护语句

    1.如何查看数据库的状态    unix下 ps -ef | grep ora windows下 看服务是否起来 是否可以连上数据库 SQL> select status, instance_r ...