Skip to content

Commit 2e6c4f7

Browse files
feat(bootstrap): Release 1.0.12: Support angular 2.0.1 and ui-router 1.0.0-beta.3+
1 parent 5cb5e11 commit 2e6c4f7

File tree

4 files changed

+122
-73
lines changed

4 files changed

+122
-73
lines changed

README.md

Lines changed: 84 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
### Hybrid Angular 1/Angular 2 UI-Router apps
44

5-
This module provides [ngUpgrade](https://angular.io/docs/ts/latest/guide/upgrade.html) support for UI-Router, allowing hybrid ng1/ng2 UI-Router apps.
5+
This module provides [ngUpgrade](https://angular.io/docs/ts/latest/guide/upgrade.html) integration
6+
with UI-Router, enabling routing to both Angular 1 and Angular 1 Components (and/or templates).
67

78
Your app will be hosted on Angular 1 while you incrementally upgrade it to Angular 2.
8-
With `ng1-to-ng2`, you can use either an Angular 1 or Angular 2 Component as a view in your state definitions.
9+
With `ui-router-ng1-to-ng2`, you can use either an Angular 1 or Angular 2 component as a view in your state definitions.
910

1011
```js
1112
import { Ng2AboutComponent } from "./about.ng2.component";
@@ -15,26 +16,28 @@ import { Ng2AboutComponent } from "./about.ng2.component";
1516
$stateProvider.state({
1617
name: 'home',
1718
url: '/home',
18-
component: 'ng1HomeComponent' // ng1 component name
19+
component: 'ng1HomeComponent' // ng1 component or directive name
1920
})
2021

2122
.state({
2223
name: 'about',
2324
url: '/about',
2425
component: Ng2AboutComponentClass // ng2 component class reference
2526
});
26-
```
2727

28-
When routing to an ng2 component, that component uses the
29-
[ng2 directives (ui-view and uiSref) from `ui-router-ng2`](https://ui-router.github.io/docs/latest/modules/ng2_directives.html).
30-
We do not yet support nesting an angular 1 directive inside an angular 2 ui-view.
31-
Because of this, apps should be migrated starting from leaf views and work up towards the root state.
28+
.state({
29+
name: 'other',
30+
url: '/other',
31+
template: '<h1>Other</h1>', // ng1 template/controller
32+
controller: function($scope) { /* do stuff */ }
33+
})
3234

33-
See the [hybrid sample app](https://github.com/ui-router/sample-app-ng1-to-ng2) for full example.
35+
```
3436

37+
When routing to an ng2 component, that ng2 component uses the standard
38+
[ng2 directives (ui-view and uiSref) from `ui-router-ng2`](https://ui-router.github.io/docs/latest/modules/ng2_directives.html).
3539

36-
*NOTE: ui-router-ng1-to-ng2 version 1.0.11 currently supports only Angular 2.0.0-rc.5*
37-
See https://github.com/ui-router/ng1-to-ng2/issues/13
40+
See the [hybrid sample app](https://github.com/ui-router/sample-app-ng1-to-ng2) for a full example.
3841

3942
### Getting started
4043

@@ -43,38 +46,50 @@ Add `@angular`, `ui-router-ng2`, and `ui-router-ng1-to-ng2` to your package.json
4346
```
4447
dependencies: {
4548
...
46-
"@angular/common": "=2.0.0-rc.5",
47-
"@angular/compiler": "=2.0.0-rc.5",
48-
"@angular/core": "=2.0.0-rc.5",
49-
"@angular/platform-browser": "=2.0.0-rc.5",
50-
"@angular/platform-browser-dynamic": "=2.0.0-rc.5",
51-
"@angular/upgrade": "=2.0.0-rc.5",
49+
"@angular/common": "~2.0.1",
50+
"@angular/compiler": "~2.0.1",
51+
"@angular/core": "~2.0.1",
52+
"@angular/platform-browser": "~2.0.1",
53+
"@angular/platform-browser-dynamic": "~2.0.1",
54+
"@angular/upgrade": "~2.0.1",
5255
...
53-
"ui-router-ng2": "^1.0.0-beta.2",
54-
"ui-router-ng1-to-ng2": "^1.0.11",
56+
"ui-router-ng2": "^1.0.0-beta.3",
57+
"ui-router-ng1-to-ng2": "^1.0.12",
5558
}
5659
```
5760

58-
#### Switch your app from bootstrapping using `ng-app` to using the ngUpgrade manual bootstrap
61+
#### Bootstrapping a hybrid app
5962

63+
Switch your app from bootstrapping using `ng-app` to using the `ngUpgrade` manual bootstrap
64+
65+
```js
66+
// Add 'ui.router.upgrade' to your ng1 app module's depedencies
67+
let ng1module = angular.module("myApp", [uiRouter, 'ui.router.upgrade']);
6068
```
69+
70+
```js
71+
// Create an Angular 2 root NgModule
72+
@NgModule({
73+
// import the Ng1ToNg2Module
74+
imports: [ BrowserModule, Ng1ToNg2Module ]
75+
}) class SampleAppModule {}
76+
6177
// Create an upgrade adapter instance
6278
import {UpgradeAdapter} from '@angular/upgrade';
63-
export const upgradeAdapter = new UpgradeAdapter();
79+
let upgradeAdapter = new UpgradeAdapter(SampleAppModule);
6480

6581
// Supply ui-router-ng1-to-ng2 with the upgrade adapter
6682
import {uiRouterNgUpgrade} from "ui-router-ng1-to-ng2";
6783
uiRouterNgUpgrade.setUpgradeAdapter(upgradeAdapter);
6884

6985
// Manually bootstrap the app with the Upgrade Adapter (instead of ng-app)
70-
// Add 'ui.router.upgrade' to your apps module depedencies
71-
upgradeAdapter.bootstrap(document.body, ['myApp', 'ui.router.upgrade']);
86+
upgradeAdapter.bootstrap(document.body, ['myApp']);
7287
```
7388

89+
#### Route to ng2 components
7490

75-
#### Route to components
76-
77-
Use `component:` in your state declaration
91+
Register states using either Angular 1 or Angular 2 code.
92+
Use `component:` in your state declaration.
7893

7994
```
8095
var leaf = {
@@ -85,12 +100,49 @@ var leaf = {
85100
$stateProvider.state(leaf);
86101
```
87102

88-
#### Declare ui-view at the top level of your application
103+
#### Create ng2 Feature Modules (optional)
104+
105+
```js
106+
@NgModule({
107+
imports: [
108+
UIRouterModule.forChild({
109+
states: [featureState1, featureState2]
110+
})
111+
],
112+
declarations: [FeatureComponent1, FeatureComponent2]
113+
})
114+
export class MyFeatureModule {}
115+
```
116+
117+
Add the feature module to the root NgModule imports
118+
```js
119+
@NgModule({
120+
// import the Ng1ToNg2Module
121+
imports: [ BrowserModule, Ng1ToNg2Module, MyFeatureModule ]
122+
}) class SampleAppModule {}
89123
```
90-
<html>
91-
<body>
92-
<ui-view></ui-view>
93-
</body>
94-
</html>
124+
125+
Note: You can also add states directly to the root NgModule using `UIRouterModule.forChild`
126+
```js
127+
@NgModule({
128+
// import the Ng1ToNg2Module and create a UIRouter "child module"
129+
imports: [
130+
BrowserModule,
131+
Ng1ToNg2Module,
132+
UIRouterModule.forChild({ states: NG2_STATES })
133+
],
134+
declarations: [NG2_COMPONENTS]
135+
}) class SampleAppModule {}
95136
```
96137

138+
### Limitations:
139+
140+
We currently support creating a `<ui-view>` in an Angular 1 view,
141+
then routing to Angular 1 or Angular 2 components inside that angular 1 `ui-view`.
142+
143+
We do not (yet) support creating a nested `ui-view` in Angular 2, then routing to an angular 1 component.
144+
Once an Angular 2 component has been routed to, any `<ui-view>` inside the Angular 2 component can only be
145+
targeted by other Angular 2 components.
146+
147+
Because of this, apps should be migrated starting from leaf states/views and work up towards the root state/view.
148+

ng1-to-ng2.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import * as angular from "angular";
2-
import {Ng1ViewConfig, $InjectorLike} from "angular-ui-router";
2+
import {Ng1ViewConfig, $InjectorLike, StateProvider, State} from "angular-ui-router";
33

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

77
import {
8-
UIRouter, ViewService, StateRegistry, StateProvider,
9-
UIView, Ng2ViewDeclaration, Ng2ViewConfig, State, PathNode, Resolvable,
10-
ParentUIViewInject, ViewConfig, forEach, UIRouterRx,
11-
NG2_INJECTOR_TOKEN, ngModuleResolvablesBuilder, UIRouterLibraryModule
8+
UIRouter, ViewService, StateRegistry,
9+
UIView, Ng2ViewDeclaration, Ng2ViewConfig, PathNode, Resolvable,
10+
ParentUIViewInject, ViewConfig, forEach, UIRouterRx,
11+
NATIVE_INJECTOR_TOKEN, _UIROUTER_SERVICE_PROVIDERS, UIRouterModule, UIROUTER_ROOT_MODULE
1212
} from "ui-router-ng2";
1313

1414
/**
@@ -137,8 +137,12 @@ export class UIViewNgUpgrade {
137137
* This NgModule should be added to the root module of the hybrid app.
138138
*/
139139
@NgModule({
140-
imports: [UIRouterLibraryModule],
140+
imports: [UIRouterModule],
141141
declarations: [UIViewNgUpgrade],
142+
providers: [
143+
..._UIROUTER_SERVICE_PROVIDERS,
144+
{ provide: UIROUTER_ROOT_MODULE, useValue: {}, multi: true }
145+
],
142146
exports: [UIViewNgUpgrade]
143147
}) export class Ng1ToNg2Module {}
144148

@@ -171,11 +175,17 @@ function applyHybridAdapter(upgradeAdapter: UpgradeAdapter) {
171175
let $uiRouter: UIRouter = ng1Injector.get('$uiRouter');
172176
new UIRouterRx($uiRouter);
173177

174-
// Expose the root ng2 Injector as a Resolvable (on the root state).
175-
// This mirrors what ui-router-ng2 does in its initialization code
176-
const getNg2Injector = () =>
177-
ng1Injector.get('ng2.Injector');
178-
let ng2InjectorResolvable = new Resolvable(NG2_INJECTOR_TOKEN, getNg2Injector, [], { when: "EAGER" });
178+
// Expose a merged ng1/ng2 injector as a Resolvable (on the root state).
179+
// This mimics how ui-router-ng2 exposes the root ng2 Injector, but
180+
// it retrieves from ng1 injector first, then ng2 injector if the token isn't found.
181+
const mergedInjector = {
182+
get: function(token: any, ng2NotFoundValue?: any) {
183+
let ng2Injector = ng1Injector.get('ng2.Injector');
184+
return (ng1Injector.has(token) && ng1Injector.get(token)) || ng2Injector.get(token, ng2NotFoundValue)
185+
}
186+
};
187+
188+
let ng2InjectorResolvable = Resolvable.fromData(NATIVE_INJECTOR_TOKEN, mergedInjector);
179189
$uiRouter.stateRegistry.root().resolvables.push(ng2InjectorResolvable);
180190
}]);
181191

@@ -209,8 +219,6 @@ function applyHybridAdapter(upgradeAdapter: UpgradeAdapter) {
209219
});
210220
return views;
211221
});
212-
213-
$stateProvider.decorator('resolvables', ngModuleResolvablesBuilder);
214222
}]);
215223

216224

package.json

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ui-router-ng1-to-ng2",
3-
"version": "1.0.11",
3+
"version": "1.0.12",
44
"scripts": {
55
"all": "npm run clean && npm run build && npm run bundle",
66
"clean": "rm -rf ng1-to-ng2.d.ts ng1-to-ng2*.js ng1-to-ng2*.js.map build",
@@ -12,31 +12,29 @@
1212
"license": "MIT",
1313
"peerDependencies": {
1414
"angular": "^1.5.0",
15-
"@angular/core": "~2.0.0",
16-
"@angular/upgrade": "~2.0.0",
17-
"core-js": "^2.4.1",
15+
"@angular/core": "~2.0.1",
16+
"@angular/upgrade": "~2.0.1",
1817
"rxjs": "~5.0.0-beta.12",
19-
"angular-ui-router": "^1.0.0-beta.2",
20-
"ui-router-ng2": "^1.0.0-beta.2",
18+
"angular-ui-router": "^1.0.0-beta.3",
19+
"ui-router-ng2": "^1.0.0-beta.3",
2120
"zone.js": "~0.6.21"
2221
},
2322
"devDependencies": {
24-
"@angular/common": "~2.0.0",
25-
"@angular/compiler": "~2.0.0",
26-
"@angular/core": "~2.0.0",
27-
"@angular/platform-browser": "~2.0.0",
28-
"@angular/platform-browser-dynamic": "~2.0.0",
29-
"@angular/upgrade": "~2.0.0",
23+
"@angular/common": "~2.0.1",
24+
"@angular/compiler": "~2.0.1",
25+
"@angular/core": "~2.0.1",
26+
"@angular/platform-browser": "~2.0.1",
27+
"@angular/platform-browser-dynamic": "~2.0.1",
28+
"@angular/upgrade": "~2.0.1",
3029
"@types/angular": "^1.5.14",
31-
"@types/core-js": "^0.9.28",
3230
"@types/jquery": "^1.10.31",
33-
"@types/node": "^6.0.31",
3431
"angular": "~1.5.3",
35-
"angular-ui-router": "^1.0.0-beta.2",
32+
"angular-ui-router": "^1.0.0-beta.3",
33+
"awesome-typescript-loader": "^2.2.4",
3634
"rxjs": "~5.0.0-beta.12",
3735
"systemjs": "0.19.25",
3836
"typescript": "^2.0.2",
39-
"ui-router-ng2": "^1.0.0-beta.2",
37+
"ui-router-ng2": "^1.0.0-beta.3",
4038
"webpack": "^1.13.1",
4139
"zone.js": "~0.6.21"
4240
},

tsconfig.json

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"target": "es5",
44
"module": "commonjs",
55
"moduleResolution": "node",
6+
"lib": ["es6", "dom"],
67
"sourceMap": true,
78
"pretty": true,
89
"declaration": true,
@@ -12,17 +13,7 @@
1213
"removeComments": false,
1314
"noImplicitAny": false,
1415
"suppressExcessPropertyErrors": true,
15-
"suppressImplicitAnyIndexErrors": true,
16-
"types": [
17-
"core-js",
18-
"node",
19-
"jquery",
20-
"angular"
21-
]
16+
"suppressImplicitAnyIndexErrors": true
2217
},
23-
"exclude": [
24-
"node_modules"
25-
],
26-
"compileOnSave": false,
27-
"buildOnSave": false
18+
"exclude": [ "node_modules" ]
2819
}

0 commit comments

Comments
 (0)