fix(upgrade/static): ensure upgraded injector is initialized early enough (#14065)

This change ensures that the upgraded AngularJS injector is initialized
before the application run blocks are executed.

Closes #13811
This commit is contained in:
Pete Bacon Darwin 2017-01-24 22:48:03 +00:00 committed by Alex Rickabaugh
parent b2f9d56577
commit 6152eb24bc
2 changed files with 30 additions and 3 deletions

View File

@ -150,10 +150,12 @@ export class UpgradeModule {
*/
bootstrap(
element: Element, modules: string[] = [], config?: any /*angular.IAngularBootstrapConfig*/) {
const INIT_MODULE_NAME = UPGRADE_MODULE_NAME + '.init';
// Create an ng1 module to bootstrap
const upgradeModule =
const initModule =
angular
.module(UPGRADE_MODULE_NAME, modules)
.module(INIT_MODULE_NAME, [])
.value(INJECTOR_KEY, this.injector)
@ -205,6 +207,8 @@ export class UpgradeModule {
}
]);
const upgradeModule = angular.module(UPGRADE_MODULE_NAME, [INIT_MODULE_NAME].concat(modules));
// Make sure resumeBootstrap() only exists if the current bootstrap is deferred
const windowAngular = (window as any /** TODO #???? */)['angular'];
windowAngular.resumeBootstrap = undefined;

View File

@ -6,11 +6,12 @@
* found in the LICENSE file at https://angular.io/license
*/
import {InjectionToken, NgModule, destroyPlatform} from '@angular/core';
import {InjectionToken, Injector, NgModule, destroyPlatform} from '@angular/core';
import {async} from '@angular/core/testing';
import {BrowserModule} from '@angular/platform-browser';
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import * as angular from '@angular/upgrade/src/angular_js';
import {$INJECTOR, INJECTOR_KEY} from '@angular/upgrade/src/aot/constants';
import {UpgradeModule, downgradeInjectable} from '@angular/upgrade/static';
import {bootstrap, html} from '../test_helpers';
@ -76,5 +77,27 @@ export function main() {
expect(ng2Injector.get(Ng1Service)).toBe('ng1 service value');
});
}));
it('should initialize the upgraded injector before application run blocks are executed',
async(() => {
let runBlockTriggered = false;
const ng1Module = angular.module('ng1Module', []).run([
INJECTOR_KEY,
function(injector: Injector) {
runBlockTriggered = true;
expect(injector.get($INJECTOR)).toBeDefined();
}
]);
@NgModule({imports: [BrowserModule, UpgradeModule]})
class Ng2Module {
ngDoBootstrap() {}
}
bootstrap(platformBrowserDynamic(), Ng2Module, html('<div>'), ng1Module).then(() => {
expect(runBlockTriggered).toBeTruthy();
});
}));
});
}