Skip to content

Commit d2d8058

Browse files
docs(README): update readme for UIRouterUpgradeModule
1 parent 5a45b1a commit d2d8058

1 file changed

Lines changed: 41 additions & 38 deletions

File tree

README.md

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ import { Ng2AboutComponentClass } from "./about.ng2.component";
1717
/// ...
1818

1919
$stateProvider.state({
20-
name: 'home',
20+
name: 'home',
2121
url: '/home',
2222
component: 'ng1HomeComponent' // AngularJS component or directive name
2323
})
2424

2525
.state({
26-
name: 'about',
26+
name: 'about',
2727
url: '/about',
2828
component: Ng2AboutComponentClass // Angular component class reference
2929
});
@@ -43,17 +43,11 @@ When routing to an Angular component, that component uses the standard
4343
When routing to an AngularJS component or template, that component uses the standard
4444
[AngularJS directives (ui-view and ui-sref) from `@uirouter/angularjs`](https://ui-router.github.io/ng1/docs/latest/modules/directives.html).
4545

46-
See the [hybrid sample app](https://github.com/ui-router/sample-app-ng1-to-ng2) for a full example.
47-
48-
### UpgradeAdapter vs UpgradeModule
49-
50-
Version 2.0.0 of `@uirouter/angular-hybrid` only supports `UpgradeAdapter`, which works fine but is no longer in development.
51-
Version 3.0.0 of `@uirouter/angular-hybrid` will support only `UpgradeModule` from `@angular/upgrade/static`, which is what the Angular team actively supports for hybrid mode.
52-
Because we are dropping support for `UpgradeAdapter`, current users will have to switch to `UpgradeModule`.
46+
See the [hybrid sample app](https://github.com/ui-router/sample-app-angular-hybrid) for a full example.
5347

5448
### Getting started
5549

56-
Remove `angular-ui-router` (or `@uirouter/angularjs`) from your package.json and replace it with `@uirouter/angular-hybrid`.
50+
Remove `angular-ui-router` (or `@uirouter/angularjs`) from your AngularJS app's package.json and replace it with `@uirouter/angular-hybrid`.
5751
Add the `@angular/*` dependencies.
5852

5953
```
@@ -86,26 +80,33 @@ let ng1module = angular.module("myApp", ['ui.router', 'ui.router.upgrade']);
8680

8781
#### Create a root Angular NgModule
8882

89-
- Import the `BrowserModule`, `UpgradeModule`, and the `UIRouterUpgradeModule`.
90-
91-
- Any AngularJS services you want to expose to Angular should have a `providers` entry.
83+
- Import the `BrowserModule`, `UpgradeModule`, and a `UIRouterUpgradeModule.forChild()` module.
84+
- Add `providers` entry for any AngularJS services you want to expose to Angular.
85+
- The module should have a `ngDoBootstrap` method which calls the `UpgradeModule`'s `bootstrap` method.
9286

93-
- The module should have a no-op `ngDoBootstrap` method.
9487

9588
```js
9689
export function getDialogService($injector) {
9790
return $injector.get('DialogService');
9891
}
9992

10093
@NgModule({
101-
imports: [ BrowserModule, UpgradeModule, UIRouterUpgradeModule ],
94+
imports: [
95+
BrowserModule,
96+
UpgradeModule,
97+
UIRouterUpgradeModule.forChild({ states: ngHybridStates }),
98+
],
10299
providers: [
103100
{ provide: NgModuleFactoryLoader, useClass: SystemJsNgModuleLoader },
104101
// Register some AngularJS services as Angular providers
105102
{ provide: 'DialogService', deps: ['$injector'], useFactory: getDialogService },
106103
]
107104
}) export class SampleAppModule {
108-
ngDoBootstrap() { /* no body */ }
105+
constructor(private upgrade: UpgradeModule) { }
106+
107+
ngDoBootstrap() {
108+
this.upgrade.bootstrap(document.body, [sampleAppModuleAngularJS.name]);
109+
}
109110
}
110111
```
111112

@@ -126,24 +127,16 @@ ngmodule.config([ '$urlServiceProvider', ($urlService: UrlService) => $urlServic
126127
#### Bootstrap the app
127128

128129
- Bootstrap Angular
129-
130-
- Then, bootstrap AngularJS
131-
132-
- Tell UIRouter to synchronize the URL and listen for further URL changes
130+
- Angular runs ngDoBootstrap() which bootstraps AngularJS
131+
- Chain off `bootstrapModule()` and tell UIRouter to synchronize the URL and listen for further URL changes
133132

134133
```js
135134
// Wait until the DOM is ready
136135
angular.element(document).ready(function () {
137136
// Manually bootstrap the Angular app
138137
platformBrowserDynamic().bootstrapModule(SampleAppModule).then(platformRef => {
139-
const injector: Injector = platformRef.injector;
140-
const upgrade = injector.get(UpgradeModule) as UpgradeModule;
141-
142-
// Manually bootstrap the AngularJS app
143-
upgrade.bootstrap(document.body, ['demo']);
144-
145-
// Intialize the Angular Module (get() any UIRouter service from DI to initialize it)
146-
const url: UrlService = injector.get(UrlService);
138+
// get() UrlService from DI (this call will create all the UIRouter services)
139+
const url: UrlService = platformRef.injector.get(UrlService);
147140

148141
// Instruct UIRouter to listen to URL changes
149142
url.listen();
@@ -159,14 +152,14 @@ angular.element(document).ready(function () {
159152
Your existing AngularJS routes work the same as before.
160153

161154
```
162-
var foo = {
155+
var foo = {
163156
name: 'foo',
164157
url: '/foo',
165158
component: 'fooComponent'
166159
};
167160
$stateProvider.state(foo);
168161
169-
var bar = {
162+
var bar = {
170163
name: 'foo.bar',
171164
url: '/bar',
172165
templateUrl: '/bar.html',
@@ -181,10 +174,10 @@ Register states using either Angular or AngularJS code.
181174
Use `component:` in your state declaration.
182175

183176
```
184-
var leaf = {
177+
var leaf = {
185178
name: 'foo.bar.leaf',
186179
url: '/leaf',
187-
component: MyNg2CommponentClass
180+
component: MyNg2CommponentClass
188181
};
189182
$stateProvider.state(leaf);
190183
```
@@ -194,7 +187,7 @@ $stateProvider.state(leaf);
194187
```js
195188
@NgModule({
196189
imports: [
197-
UIRouterModule.forChild({
190+
UIRouterUpgradeModule.forChild({
198191
states: [featureState1, featureState2]
199192
})
200193
],
@@ -208,8 +201,11 @@ export class MyFeatureModule {}
208201
Add the feature module to the root NgModule imports
209202
```js
210203
@NgModule({
211-
// import the Ng1ToNg2Module
212-
imports: [ BrowserModule, Ng1ToNg2Module, MyFeatureModule ]
204+
imports: [
205+
BrowserModule,
206+
UIRouterUpgradeModule,
207+
MyFeatureModule
208+
]
213209
}) class SampleAppModule {}
214210
```
215211

@@ -222,8 +218,7 @@ Note: You can also add states directly to the root NgModule using `UIRouterModul
222218
// import the Ng1ToNg2Module and create a UIRouter "child module"
223219
imports: [
224220
BrowserModule,
225-
Ng1ToNg2Module,
226-
UIRouterModule.forChild({ states: NG2_STATES })
221+
UIRouterUpgradeModule.forChild({ states: NG2_STATES })
227222
],
228223
declarations: [NG2_COMPONENTS]
229224
}) class SampleAppModule {}
@@ -240,7 +235,8 @@ Because of this, apps should be migrated starting from leaf states/views and wor
240235

241236
---
242237

243-
When a state has an `onEnter`, `onExit`, or `onRetain`, they are always injected (AngularJS style), even if the state uses Angular 2+ components or is added to an `UIRouterModule.forChild` `NgModule`.
238+
When a state has an `onEnter`, `onExit`, or `onRetain`, they are always injected (AngularJS style),
239+
even if the state uses Angular 2+ components or is added to an `UIRouterUpgradeModule.forChild` `NgModule`.
244240

245241
```js
246242
export function ng2StateOnEnter(transition: Transition, svc: MyService) {
@@ -258,3 +254,10 @@ export const NG2_STATE = {
258254
The minimal example of `@uirouter/angular-hybrid` can be found here: https://github.com/ui-router/angular-hybrid/tree/master/example
259255

260256
A full fledged sample application example can be found here: https://github.com/ui-router/sample-app-angular-hybrid
257+
258+
# UpgradeAdapter vs UpgradeModule
259+
260+
Version 2.0.0 of `@uirouter/angular-hybrid` only supports `UpgradeAdapter`, which works fine but is no longer in development.
261+
Version 3+0.0+ of `@uirouter/angular-hybrid` will support only `UpgradeModule` from `@angular/upgrade/static`, which is what the Angular team actively supports for hybrid mode.
262+
Because we are dropping support for `UpgradeAdapter`, current users will have to switch to `UpgradeModule`.
263+

0 commit comments

Comments
 (0)