2
2
3
3
### Hybrid Angular 1/Angular 2 UI-Router apps
4
4
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).
6
7
7
8
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.
9
10
10
11
``` js
11
12
import { Ng2AboutComponent } from " ./about.ng2.component" ;
@@ -15,26 +16,28 @@ import { Ng2AboutComponent } from "./about.ng2.component";
15
16
$stateProvider .state ({
16
17
name: ' home' ,
17
18
url: ' /home' ,
18
- component: ' ng1HomeComponent' // ng1 component name
19
+ component: ' ng1HomeComponent' // ng1 component or directive name
19
20
})
20
21
21
22
.state ({
22
23
name: ' about' ,
23
24
url: ' /about' ,
24
25
component: Ng2AboutComponentClass // ng2 component class reference
25
26
});
26
- ```
27
27
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
+ })
32
34
33
- See the [ hybrid sample app ] ( https://github.com/ui-router/sample-app-ng1-to-ng2 ) for full example.
35
+ ```
34
36
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 ) .
35
39
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.
38
41
39
42
### Getting started
40
43
@@ -43,38 +46,50 @@ Add `@angular`, `ui-router-ng2`, and `ui-router-ng1-to-ng2` to your package.json
43
46
```
44
47
dependencies: {
45
48
...
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 ",
52
55
...
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 ",
55
58
}
56
59
```
57
60
58
- #### Switch your app from bootstrapping using ` ng- app` to using the ngUpgrade manual bootstrap
61
+ #### Bootstrapping a hybrid app
59
62
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' ]);
60
68
```
69
+
70
+ ``` js
71
+ // Create an Angular 2 root NgModule
72
+ @NgModule ({
73
+ // import the Ng1ToNg2Module
74
+ imports: [ BrowserModule, Ng1ToNg2Module ]
75
+ }) class SampleAppModule {}
76
+
61
77
// Create an upgrade adapter instance
62
78
import {UpgradeAdapter } from ' @angular/upgrade' ;
63
- export const upgradeAdapter = new UpgradeAdapter();
79
+ let upgradeAdapter = new UpgradeAdapter (SampleAppModule );
64
80
65
81
// Supply ui-router-ng1-to-ng2 with the upgrade adapter
66
82
import {uiRouterNgUpgrade } from " ui-router-ng1-to-ng2" ;
67
83
uiRouterNgUpgrade .setUpgradeAdapter (upgradeAdapter);
68
84
69
85
// 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' ]);
72
87
```
73
88
89
+ #### Route to ng2 components
74
90
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.
78
93
79
94
```
80
95
var leaf = {
@@ -85,12 +100,49 @@ var leaf = {
85
100
$stateProvider.state(leaf);
86
101
```
87
102
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 {}
89
123
```
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 {}
95
136
```
96
137
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
+
0 commit comments