refactor(core): fix bootstrapModule regarding zones and initializers (#10383)

This makes `bootstrapModuleFactory` wait for promises
returned by `APP_INITIALIZER`s, also making `bootstrapModuleFactory` async.
I.e. now `bootstrapModule` and `bootstrapModuleFactory` behave in the
same way.

This ensures that all code from module instantiation, to creating
`ApplicationRef`s as well as calling `APP_INITIALIZERS` is run
in the Angular zone.

This also moves the invocation of the initializers from the `ApplicationRef`
constructor into the `bootstrapModuleFactory` call, allowing initializers
to get a hold of `ApplicationRef` (see #9101).

Fixes #9101
Fixes #10363
Fixes #10205
This commit is contained in:
Tobias Bosch
2016-07-29 06:47:40 -07:00
parent 633c7d1ebe
commit a46437c57d
9 changed files with 223 additions and 211 deletions

View File

@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/
import {ApplicationRef, Compiler, CompilerFactory, ComponentFactory, ComponentResolver, Injector, NgModule, NgZone, PlatformRef, Provider, ReflectiveInjector, Testability, Type, provide} from '@angular/core';
import {ApplicationRef, Compiler, CompilerFactory, ComponentFactory, ComponentResolver, Injector, NgModule, NgModuleRef, NgZone, PlatformRef, Provider, ReflectiveInjector, Testability, Type, provide} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
@ -288,10 +288,17 @@ export class UpgradeAdapter {
class DynamicModule {
}
const compilerFactory: CompilerFactory = platformRef.injector.get(CompilerFactory);
var moduleRef = platformRef.bootstrapModuleFactory(
compilerFactory.createCompiler().compileModuleSync(DynamicModule));
platformRef.bootstrapModule(DynamicModule).then((moduleRef) => {
ng1Injector = this._afterNg2ModuleBootstrap(moduleRef, upgrade, element, modules, config);
});
return upgrade;
}
private _afterNg2ModuleBootstrap(
moduleRef: NgModuleRef<any>, upgrade: UpgradeAdapterRef, element: Element, modules?: any[],
config?: angular.IAngularBootstrapConfig): angular.IInjectorService {
const boundCompiler: Compiler = moduleRef.injector.get(Compiler);
var ng1Injector: angular.IInjectorService = null;
var applicationRef: ApplicationRef = moduleRef.injector.get(ApplicationRef);
var injector: Injector = applicationRef.injector;
var ngZone: NgZone = injector.get(NgZone);
@ -398,7 +405,7 @@ export class UpgradeAdapter {
}
});
}, onError);
return upgrade;
return ng1Injector;
}
/**