From a5205c686e463e0c9e9b49edf1856b053adbde86 Mon Sep 17 00:00:00 2001 From: Georgios Kalpakas Date: Tue, 18 Jul 2017 22:50:17 +0300 Subject: [PATCH] fix(upgrade): allow accessing AngularJS injector from downgraded module --- .../upgrade/src/static/downgrade_module.ts | 3 +- .../integration/downgrade_module_spec.ts | 38 ++++++++++++++++++- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/packages/upgrade/src/static/downgrade_module.ts b/packages/upgrade/src/static/downgrade_module.ts index f54de29d4b..d1fcabef00 100644 --- a/packages/upgrade/src/static/downgrade_module.ts +++ b/packages/upgrade/src/static/downgrade_module.ts @@ -42,10 +42,9 @@ export function downgradeModule( .factory(LAZY_MODULE_REF, [ $INJECTOR, ($injector: angular.IInjectorService) => { + setTempInjectorRef($injector); const result: LazyModuleRef = { promise: bootstrapFn(angular1Providers).then(ref => { - setTempInjectorRef($injector); - injector = result.injector = new NgAdapterInjector(ref.injector); injector.get($INJECTOR); diff --git a/packages/upgrade/test/static/integration/downgrade_module_spec.ts b/packages/upgrade/test/static/integration/downgrade_module_spec.ts index f506c77ddc..a58164bae4 100644 --- a/packages/upgrade/test/static/integration/downgrade_module_spec.ts +++ b/packages/upgrade/test/static/integration/downgrade_module_spec.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {Component, Inject, Input, NgModule, Provider, destroyPlatform} from '@angular/core'; +import {Component, Inject, Injector, Input, NgModule, Provider, destroyPlatform} from '@angular/core'; import {async} from '@angular/core/testing'; import {BrowserModule} from '@angular/platform-browser'; import {platformBrowserDynamic} from '@angular/platform-browser-dynamic'; @@ -132,5 +132,41 @@ export function main() { setTimeout(() => expect(element.textContent).toBe('foo-bar')); }); })); + + it('should give access to both injectors in the Angular module\'s constructor', async(() => { + let $injectorFromNg2: angular.IInjectorService|null = null; + + @Component({selector: 'ng2', template: ''}) + class Ng2Component { + } + + @NgModule({ + declarations: [Ng2Component], + entryComponents: [Ng2Component], + imports: [BrowserModule], + }) + class Ng2Module { + constructor(injector: Injector) { + $injectorFromNg2 = injector.get('$injector' as any); + } + + ngDoBootstrap() {} + } + + const bootstrapFn = (extraProviders: Provider[]) => + platformBrowserDynamic(extraProviders).bootstrapModule(Ng2Module); + const lazyModuleName = downgradeModule(bootstrapFn); + const ng1Module = + angular.module('ng1', [lazyModuleName]) + .directive( + 'ng2', downgradeComponent({component: Ng2Component, propagateDigest: false})) + .value('ng1Value', 'foo'); + + const element = html(''); + const $injectorFromNg1 = angular.bootstrap(element, [ng1Module.name]); + + // Wait for the module to be bootstrapped. + setTimeout(() => expect($injectorFromNg2).toBe($injectorFromNg1)); + })); }); }