Skip to content

Commit

Permalink
fix(Ng1ToNg2Module): Register UIRouterModule.forChild() states from…
Browse files Browse the repository at this point in the history
… bootstrapped NgModule

Release 1.0.15
  • Loading branch information
christopherthielen committed Sep 27, 2016
1 parent 5d9a752 commit 9d94cd6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
28 changes: 19 additions & 9 deletions ng1-to-ng2.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import * as angular from "angular";
import {Ng1ViewConfig, $InjectorLike, StateProvider, State} from "angular-ui-router";

import {ElementRef, Component, Input, Inject, NgModule} from "@angular/core";
import {ElementRef, Component, Input, Inject, NgModule, Injector} from "@angular/core";
import {UpgradeAdapter} from "@angular/upgrade";

import {
UIRouter, ViewService, StateRegistry,
UIView, Ng2ViewDeclaration, Ng2ViewConfig, PathNode, Resolvable,
ParentUIViewInject, ViewConfig, forEach, UIRouterRx,
NATIVE_INJECTOR_TOKEN, _UIROUTER_SERVICE_PROVIDERS, UIRouterModule, UIROUTER_ROOT_MODULE
NATIVE_INJECTOR_TOKEN, _UIROUTER_SERVICE_PROVIDERS, UIRouterModule, UIROUTER_ROOT_MODULE, UIROUTER_MODULE_TOKEN,
applyModuleConfig, StatesModule
} from "ui-router-ng2";

/**
Expand Down Expand Up @@ -133,24 +134,34 @@ export class UIViewNgUpgrade {
* Ng2 @NgModule and bootstrap code
**********************************/

// Register the ng1 DI '$uiRouter' object as an ng2 Provider.
function uiRouterUpgradeFactory(router: UIRouter, injector: Injector) {
let modules: StatesModule[] = injector.get(UIROUTER_MODULE_TOKEN, []);
modules.forEach(module => applyModuleConfig(router, injector, module));
return router;
}

/**
* This NgModule should be added to the root module of the hybrid app.
*/
@NgModule({
imports: [UIRouterModule],
declarations: [UIViewNgUpgrade],
providers: [
// ui-router-ng2 code will use the ng1 $uiRouter instance instead of creating its own.
{ provide: UIRouter, useFactory: uiRouterUpgradeFactory, deps: ['$uiRouter', Injector] },

{ provide: UIROUTER_ROOT_MODULE, useValue: {}, multi: true },

..._UIROUTER_SERVICE_PROVIDERS,

{
provide: UIView.PARENT_INJECT,
deps: [StateRegistry],
useFactory: (r: StateRegistry) => {
return { fqn: null, context: r.root() } as ParentUIViewInject
},
},

{ provide: UIROUTER_ROOT_MODULE, useValue: {}, multi: true },

..._UIROUTER_SERVICE_PROVIDERS,
],
exports: [UIViewNgUpgrade, UIRouterModule]
}) export class Ng1ToNg2Module {}
Expand All @@ -170,9 +181,8 @@ export class UIViewNgUpgrade {
* allows both ng1 and ng2 ui-views to activate
*/
function applyHybridAdapter(upgradeAdapter: UpgradeAdapter) {
// Register the ng1 DI '$uiRouter' instance as an ng2 Provider.
// ui-router-ng2 code will use the ng1 UIRouter instance instead of creating its own instance.
upgradeAdapter.upgradeNg1Provider('$uiRouter', { asToken: UIRouter });
// Expose the ng1 DI '$uiRouter' instance as an ng2 Provider.
upgradeAdapter.upgradeNg1Provider('$uiRouter');

// Downgrade the UIViewNgUpgrade ng2 Component to an ng1 directive.
// The directive is used in a (generated) view template by the (host) ng1 ui-router,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ui-router-ng1-to-ng2",
"version": "1.0.14",
"version": "1.0.15",
"scripts": {
"all": "npm run clean && npm run build && npm run bundle",
"clean": "rm -rf ng1-to-ng2.d.ts ng1-to-ng2*.js ng1-to-ng2*.js.map build",
Expand Down

0 comments on commit 9d94cd6

Please sign in to comment.