Skip to content

Commit bb6b591

Browse files
petebacondarwinjasonaden
authored andcommitted
docs(upgrade): use a class for upgraded service (angular#18487) (angular#18487)
This makes the resulting use in Angular more ideomatic, since we can just use the class type as the injection indicator. PR Close angular#18487 PR Close angular#18487
1 parent 4258c3d commit bb6b591

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

packages/examples/upgrade/static/ts/full/module.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
8+
// #docplaster
99
import {Component, Directive, ElementRef, EventEmitter, Inject, Injectable, Injector, Input, NgModule, Output} from '@angular/core';
1010
import {BrowserModule} from '@angular/platform-browser';
1111
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
@@ -18,6 +18,12 @@ interface Hero {
1818
description: string;
1919
}
2020

21+
// #docregion ng1-text-formatter-service
22+
class TextFormatter {
23+
titleCase(value: string) { return value.replace(/((^|\s)[a-z])/g, (_, c) => c.toUpperCase()); }
24+
}
25+
26+
// #enddocregion
2127
// #docregion Angular Stuff
2228
// #docregion ng2-heroes
2329
// This Angular component will be "downgraded" to be used in AngularJS
@@ -51,9 +57,9 @@ class HeroesService {
5157
];
5258

5359
// #docregion use-ng1-upgraded-service
54-
constructor(@Inject('titleCase') titleCase: (v: string) => string) {
60+
constructor(textFormatter: TextFormatter) {
5561
// Change all the hero names to title case, using the "upgraded" AngularJS service
56-
this.heroes.forEach((hero: Hero) => hero.name = titleCase(hero.name));
62+
this.heroes.forEach((hero: Hero) => hero.name = textFormatter.titleCase(hero.name));
5763
}
5864
// #enddocregion
5965

@@ -92,7 +98,7 @@ class Ng1HeroComponentWrapper extends UpgradeComponent {
9298
HeroesService,
9399
// #docregion upgrade-ng1-service
94100
// Register an Angular provider whose value is the "upgraded" AngularJS service
95-
{provide: 'titleCase', useFactory: (i: any) => i.get('titleCase'), deps: ['$injector']}
101+
{provide: TextFormatter, useFactory: (i: any) => i.get('textFormatter'), deps: ['$injector']}
96102
// #enddocregion
97103
],
98104
// All components that are to be "downgraded" must be declared as `entryComponents`
@@ -134,11 +140,9 @@ ng1AppModule.component('ng1Hero', {
134140
});
135141
// #enddocregion
136142

137-
// #docregion ng1-title-case-service
143+
// #docregion ng1-text-formatter-service
138144
// This AngularJS service will be "upgraded" to be used in Angular
139-
ng1AppModule.factory(
140-
'titleCase',
141-
() => (value: string) => value.replace(/((^|\s)[a-z])/g, (_, c) => c.toUpperCase()));
145+
ng1AppModule.service('textFormatter', [TextFormatter]);
142146
// #enddocregion
143147

144148
// #docregion downgrade-ng2-heroes-service

packages/upgrade/src/static/upgrade_module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ import {NgAdapterInjector} from './util';
127127
*
128128
* Let's say you have an AngularJS service:
129129
*
130-
* {@example upgrade/static/ts/full/module.ts region="ng1-title-case-service"}
130+
* {@example upgrade/static/ts/full/module.ts region="ng1-text-formatter-service"}
131131
*
132132
* Then you should define an Angular provider to be included in your `NgModule` `providers`
133133
* property.

0 commit comments

Comments
 (0)