Skip to content
This repository was archived by the owner on Dec 4, 2017. It is now read-only.

Commit e29c435

Browse files
committed
add ng1-upgraded-providers.ts, minor edits
1 parent a596bcb commit e29c435

File tree

10 files changed

+83
-45
lines changed

10 files changed

+83
-45
lines changed

public/docs/_examples/_boilerplate/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"compileOnSave": true,
1717
"exclude": [
1818
"node_modules/*",
19-
"**/*-aot.ts"
19+
"**/*-aot.ts",
20+
"aot/**/*"
2021
]
2122
}

public/docs/_examples/upgrade-module/ts/app/1-to-2-providers/app.module.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,17 @@ import { UpgradeModule, downgradeComponent } from '@angular/upgrade/static';
66

77
import { HeroDetailComponent } from './hero-detail.component';
88
import { HeroesService } from './heroes.service';
9-
109
// #docregion register
10+
import { heroesServiceProvider } from './ng1-upgraded-providers';
11+
1112
@NgModule({
1213
imports: [
1314
BrowserModule,
1415
UpgradeModule
1516
],
16-
providers: [{
17-
provide: HeroesService,
18-
useFactory: (i: any) => i.get('heroes'),
19-
deps: ['$injector']
20-
}],
17+
providers: [
18+
heroesServiceProvider
19+
],
2120
// #enddocregion register
2221
declarations: [
2322
HeroDetailComponent
@@ -39,7 +38,6 @@ angular.module('heroApp', [])
3938
downgradeComponent({component: HeroDetailComponent}) as angular.IDirectiveFactory
4039
);
4140

42-
4341
platformBrowserDynamic().bootstrapModule(AppModule).then(platformRef => {
4442
const upgrade = platformRef.injector.get(UpgradeModule) as UpgradeModule;
4543
upgrade.bootstrap(document.body, ['heroApp'], {strictDi: true});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// #docregion
2+
import { HeroesService } from './heroes.service';
3+
4+
export function heroesServiceFactory(i: any) {
5+
return i.get('heroes');
6+
}
7+
8+
export const heroesServiceProvider = {
9+
provide: HeroesService,
10+
useFactory: heroesServiceFactory,
11+
deps: ['$injector']
12+
};

public/docs/_examples/upgrade-phonecat-2-hybrid/ts/app/app.module.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ import { CheckmarkPipe } from './core/checkmark/checkmark.pipe';
2121
// #docregion phonelist
2222
import { PhoneListComponent } from './phone-list/phone-list.component';
2323
// #enddocregion phonelist
24+
// #docregion routeparams
25+
import { routeParamsProvider } from './ng1-upgraded-providers';
26+
// #enddocregion routeparams
2427
// #docregion phonedetail
2528
import { PhoneDetailComponent } from './phone-detail/phone-detail.component';
2629
// #enddocregion phonedetail
@@ -57,11 +60,7 @@ import { PhoneDetailComponent } from './phone-detail/phone-detail.component';
5760
providers: [
5861
Phone,
5962
// #enddocregion phone
60-
{
61-
provide: '$routeParams',
62-
useFactory: routeParamsFactory,
63-
deps: ['$injector']
64-
}
63+
routeParamsProvider
6564
// #docregion phone
6665
]
6766
// #enddocregion routeparams
@@ -73,9 +72,3 @@ export class AppModule {
7372
// #docregion bare
7473
}
7574
// #enddocregion bare, upgrademodule, httpmodule, phone, phonelist, phonedetail, checkmarkpipe
76-
77-
// #docregion routeparams
78-
export function routeParamsFactory(i: any) {
79-
return i.get('$routeParams');
80-
}
81-
// #enddocregion routeparams
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// #docregion
2+
export abstract class RouteParams {
3+
[key: string]: string;
4+
}
5+
6+
export function routeParamsFactory(i: any) {
7+
return i.get('$routeParams');
8+
}
9+
10+
export const routeParamsProvider = {
11+
provide: RouteParams,
12+
useFactory: routeParamsFactory,
13+
deps: ['$injector']
14+
};

public/docs/_examples/upgrade-phonecat-2-hybrid/ts/app/phone-detail/phone-detail.component.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ declare var angular: angular.IAngularStatic;
44
import { downgradeComponent } from '@angular/upgrade/static';
55

66
// #docregion initialclass
7-
import { Component, Inject } from '@angular/core';
7+
import { Component } from '@angular/core';
88

99
import { Phone, PhoneData } from '../core/phone/phone.service';
1010
// #enddocregion initialclass
11-
// #docregion checkmark-pipe
11+
import { RouteParams } from '../ng1-upgraded-providers';
1212

1313
// #docregion initialclass
1414
@Component({
@@ -18,13 +18,12 @@ import { Phone, PhoneData } from '../core/phone/phone.service';
1818
// #enddocregion initialclass
1919
// #docregion initialclass
2020
})
21-
// #enddocregion checkmark-pipe
2221
export class PhoneDetailComponent {
2322
phone: PhoneData;
2423
mainImageUrl: string;
2524

26-
constructor(@Inject('$routeParams') $routeParams: any, phone: Phone) {
27-
phone.get($routeParams['phoneId']).subscribe(phone => {
25+
constructor(routeParams: RouteParams, phone: Phone) {
26+
phone.get(routeParams['phoneId']).subscribe(phone => {
2827
this.phone = phone;
2928
this.setImage(phone.images[0]);
3029
});

public/docs/_examples/upgrade-phonecat-3-router/ts/app/app.module.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { Phone } from './core/phone/phone.service';
1111
import { CheckmarkPipe } from './core/checkmark/checkmark.pipe';
1212
import { PhoneListComponent } from './phone-list/phone-list.component';
1313
import { PhoneDetailComponent } from './phone-detail/phone-detail.component';
14+
import { routeParamsProvider } from './ng1-upgraded-providers';
1415

1516
@NgModule({
1617
imports: [
@@ -32,18 +33,10 @@ import { PhoneDetailComponent } from './phone-detail/phone-detail.component';
3233
],
3334
providers: [
3435
Phone,
35-
{
36-
provide: '$routeParams',
37-
useFactory: routeParamsFactory,
38-
deps: ['$injector']
39-
}
36+
routeParamsProvider
4037
],
4138
// #docregion bootstrap
4239
bootstrap: [ AppComponent ]
4340
})
4441
export class AppModule { }
4542
// #enddocregion bootstrap
46-
47-
export function routeParamsFactory(i: any) {
48-
return i.get('$routeParams');
49-
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// #docregion
2+
export abstract class RouteParams {
3+
[key: string]: string;
4+
}
5+
6+
export function routeParamsFactory(i: any) {
7+
return i.get('$routeParams');
8+
}
9+
10+
export const routeParamsProvider = {
11+
provide: RouteParams,
12+
useFactory: routeParamsFactory,
13+
deps: ['$injector']
14+
};

public/docs/_examples/upgrade-phonecat-3-router/ts/app/phone-detail/phone-detail.component.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { downgradeComponent } from '@angular/upgrade/static';
55
import { Component, Inject } from '@angular/core';
66

77
import { Phone, PhoneData } from '../core/phone/phone.service';
8+
import { RouteParams } from '../ng1-upgraded-providers';
89

910
@Component({
1011
moduleId: module.id,
@@ -14,8 +15,8 @@ export class PhoneDetailComponent {
1415
phone: PhoneData;
1516
mainImageUrl: string;
1617

17-
constructor(@Inject('$routeParams') $routeParams: any, phone: Phone) {
18-
phone.get($routeParams['phoneId']).subscribe(phone => {
18+
constructor(routeParams: RouteParams, phone: Phone) {
19+
phone.get(routeParams['phoneId']).subscribe(phone => {
1920
this.phone = phone;
2021
this.setImage(phone.images[0]);
2122
});

public/docs/ts/latest/guide/upgrade.jade

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -730,8 +730,16 @@ figure
730730

731731
:marked
732732
We can upgrade the service using a Angular 2 [Factory provider](./dependency-injection.html#factory-providers)
733-
that requests the service from the Angular 1 `$injector`:
733+
that requests the service from the Angular 1 `$injector`.
734734

735+
We recommend declaring the Factory Provider in a separate `ng1-upgraded-providers.ts` file
736+
so that they are all together, making it easier to reference them, create new ones and
737+
delete them once the upgrade is over.
738+
739+
It's also recommended to export the `heroesServiceFactory` function so that Ahead-of-Time
740+
compilation can pick it up.
741+
742+
+makeExample('upgrade-module/ts/app/1-to-2-providers/ng1-upgraded-providers.ts', null, 'ng1-upgraded-providers.ts')
735743
+makeExample('upgrade-module/ts/app/1-to-2-providers/app.module.ts', 'register', 'app.module.ts')
736744

737745
:marked
@@ -1378,7 +1386,7 @@ code-example(format="").
13781386

13791387
:marked
13801388
This is similar to the phone list component.
1381-
The new wrinkle is the `@Inject` decorator that identifies the `$routeParams` dependency.
1389+
The new wrinkle is the `RouteParams` type annotation that identifies the `routeParams` dependency.
13821390

13831391
The Angular 1 injector has an Angular 1 router dependency called `$routeParams`.
13841392
which was injected into `PhoneDetails` when it was still an Angular 1 controller.
@@ -1387,9 +1395,10 @@ code-example(format="").
13871395
Unfortunately, Angular 1 dependencies are not automatically available to Angular 2 components.
13881396
We must use a [Factory provider](#making-angular-1-dependencies-injectable-to-angular-2)
13891397
to make `$routeParams` an Angular 2 provider.
1390-
Do that in `app.module.ts`:
1398+
Do that in a new file called `ng1-upgraded-providers.ts` and import it in `app.module.ts`:
13911399

1392-
+makeExample('upgrade-phonecat-2-hybrid/ts/app/app.module.ts', 'routeparams', 'app/app.module.ts ($routeParms)')(format='.')
1400+
+makeExample('upgrade-phonecat-2-hybrid/ts/app/ng1-upgraded-providers.ts', null, 'app/ng1-upgraded-providers.ts')
1401+
+makeExample('upgrade-phonecat-2-hybrid/ts/app/app.module.ts', 'routeparams', 'app/app.module.ts ($routeParams)')(format='.')
13931402

13941403
:marked
13951404
Convert the phone detail component template into Angular 2 syntax as follows:
@@ -1538,6 +1547,9 @@ code-example(format="").
15381547
We can also remove the `ngDoBootstrap()` override from `app.module.ts` since we are now
15391548
bootstrapping from Angular 2.
15401549

1550+
And since `PhoneListComponent` isn't being rendered from a `<phone-list>` tag anymore,
1551+
but rather routed to, we can do away with it's Angular 2 selector as well.
1552+
15411553
+makeExample('upgrade-phonecat-3-router/ts/app/app.module.ts', null, 'app/app.module.ts')
15421554

15431555
:marked
@@ -1601,10 +1613,13 @@ code-example(format="").
16011613

16021614
:marked
16031615
If you haven't already, remove all references to the `UpgradeModule` from `app.module.ts`,
1604-
as well as any [Factory provider](#making-angular-1-dependencies-injectable-to-angular-2) for Angular 1 services.
1605-
1606-
Also remove any `downgradeInjectable` or `downgradeComponent()` you find,
1616+
as well as any [Factory provider](#making-angular-1-dependencies-injectable-to-angular-2)
1617+
for Angular 1 services, and the `app/ng1-upgraded-providers.ts` file.
1618+
1619+
Also remove any `downgradeInjectable()` or `downgradeComponent()` you find,
16071620
together with the associated Angular 1 factory or directive declarations.
1621+
Since we have no downgraded components anymore, we also don't need to have them listed
1622+
in `entryComponents` either.
16081623

16091624
+makeExample('upgrade-phonecat-4-final/ts/app/app.module.ts', null, 'app.module.ts')
16101625

@@ -1621,9 +1636,7 @@ code-example(format="").
16211636

16221637
The external typings for Angular 1 may be uninstalled as well. The only ones
16231638
we still need are for Jasmine and Angular 2 polyfills.
1624-
1625-
The `@angular/upgrade` package and it's mapping in `systemjs.config.js` can also go,
1626-
we don't need them anymore.
1639+
The `@angular/upgrade` package and it's mapping in `systemjs.config.js` can also go.
16271640

16281641
code-example(format="").
16291642
npm uninstall @angular/upgrade --save

0 commit comments

Comments
 (0)