Skip to content

Commit

Permalink
fix(upgrade): allow accessing AngularJS injector from downgraded module
Browse files Browse the repository at this point in the history
  • Loading branch information
gkalpak authored and alxhub committed Jul 18, 2017
1 parent 8076482 commit a5205c6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
3 changes: 1 addition & 2 deletions packages/upgrade/src/static/downgrade_module.ts
Expand Up @@ -42,10 +42,9 @@ export function downgradeModule<T>(
.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);

Expand Down
Expand Up @@ -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';
Expand Down Expand Up @@ -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<angular.IInjectorService>('$injector' as any);
}

ngDoBootstrap() {}
}

const bootstrapFn = (extraProviders: Provider[]) =>
platformBrowserDynamic(extraProviders).bootstrapModule(Ng2Module);
const lazyModuleName = downgradeModule<Ng2Module>(bootstrapFn);
const ng1Module =
angular.module('ng1', [lazyModuleName])
.directive(
'ng2', downgradeComponent({component: Ng2Component, propagateDigest: false}))
.value('ng1Value', 'foo');

const element = html('<ng2></ng2>');
const $injectorFromNg1 = angular.bootstrap(element, [ng1Module.name]);

// Wait for the module to be bootstrapped.
setTimeout(() => expect($injectorFromNg2).toBe($injectorFromNg1));
}));
});
}

0 comments on commit a5205c6

Please sign in to comment.