相关疑难解决方法(0)

Angular2使用外部json配置文件进行异步引导

我已升级到angular2 RC6,并希望在引导我的AppModule之前加载外部JSON配置文件.我在RC5之前有这个工作,但我现在很难找到一种注入这些数据的等效方法.

/** Create dummy XSRF Strategy for Http. */
const XRSF_MOCK = provide(XSRFStrategy, { provide: XSRFStrategy, useValue:  new FakeXSRFStrategyService() });

/** Create new DI. */
var injector = ReflectiveInjector.resolveAndCreate([ConfigService, HTTP_PROVIDERS, XRSF_MOCK]);

/** Get Http via DI. */
var http = injector.get(Http);

/** Http load config file before bootstrapping app. */
http.get('./config.json').map(res => res.json())
    .subscribe(data => {

        /** Load JSON response into ConfigService. */
        let jsonConfig: ConfigService = new ConfigService();
        jsonConfig.fromJson(data);

        /** Bootstrap AppCOmponent. */
        bootstrap(AppComponent, [..., provide(ConfigService, { useValue: …
Run Code Online (Sandbox Code Playgroud)

json config angular

6
推荐指数
1
解决办法
2905
查看次数

带有来自ajax调用的数据的angular2引导程序

我想用我从服务中检索的数据来引导我的应用程序.我正在做一些事情

let dependencies = [
    //... a load of dependencies
    MyService
];

let injector = Injector.resolveAndCreate(dependencies);
let service: MyService = injector.get(MyService);

service.getData() // returns observable
    .toPromise()
    .then((d) => {
        // use data to append to dependencies

        bootstrap(App, dependencies)
    });
Run Code Online (Sandbox Code Playgroud)

这工作正常,但我不喜欢两次使用依赖数组,有没有更简洁的方法这样做?我可以在bootstrap之后向应用程序注入器添加内容吗?另外我注意到bootstrap函数返回一个promise,我可以使用这个promise来阻止应用程序的引导直到我的ajax请求完成之后吗?

当然,对于Injector我来说,我只能使用那些所需的依赖关系,MyService但是这使得它非常脆弱,你可以想象.

typescript angular

5
推荐指数
1
解决办法
4270
查看次数

标签 统计

angular ×2

config ×1

json ×1

typescript ×1