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. C++函数传递数组的两种方式

    数组与指针. 传首地址过去,然后通过地址输出数组元素. 1.一维数组 #include<iostream> using namespace std; #include <cstrin ...

  2. 关于thinkpadU盘系统盘启动不了解决方法

    http://www.laomaotao.org/softhelp/bios/382.html(原文章地址,比较全面) thinkpad笔记本uefi无法启动详细解决教程 最近有个别用户反映说thin ...

  3. HDU 4339 Contest 4

    树状数组,主要是抓住要求连续1的个数.这样,初始时,相同的加1,不同的加0. 查询时,用二分搜索右边界.就是比较当前mid-l+1的值与他们之间1的个数(这可以通过树状数组求区间和得出),记录右边界即 ...

  4. IntelliJ IDEA启动spring boot项目出现Failed to start component [StandardEngine[Tomcat].StandardHost[localhost].TomcatEmbeddedContext[]]

    IntelliJ IDEA启动spring boot项目出现Failed to start component [StandardEngine[Tomcat].StandardHost[localho ...

  5. vue中export default 在console中是this.$vm

    vue中export default 在console中是this.$vm 用vue-cli搭出框架,用webstorm进行开发,参考vue2的官网进行教程学习, 在vue-cli中是用es6的exp ...

  6. [Angular] Send Data via HTTP using Angular HttpParams

    Obviously in a real world application we do not only fetch data from the backend, but we also send d ...

  7. 排序(3)---------冒泡排序(C语言实现)

    说到冒泡排序,大一的时候第一次学习这个排序算法,可能大家不知道,"冒泡"在我说的方言里面是吹牛逼的意思. 所以就认为这个排序算法特吹牛逼有木有. 相信大家对全部的排序算法,这个想必 ...

  8. IOS7 textkit 的相关

    去年基于5.0开发的时候.自己用coreText编写了一个富文本,全部的效果都实现的非常好.可是没有去測试效率.只是在cell重用的时候表现不错.在4s上面也不会卡顿. 唯一一个问题就是,在使用AL的 ...

  9. hadoop相关

    执行wordcount 代码 package org.apache.hadoop.examples; import java.io.IOException; import java.util.Iter ...

  10. mysql表空间传输(ERROR 1808) row_format设置

    文章结构如下: 从MYSQL5.6版本开始,引入了传输表空间这个功能,可以把一张表从一个数据库移到另一个数据库或者机器上.迁移的时候很方便,尤其是大表. 由于本次达到测试使用版本5.6.38传到5.7 ...