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
99import { Component , Directive , ElementRef , EventEmitter , Inject , Injectable , Injector , Input , NgModule , Output } from '@angular/core' ;
1010import { BrowserModule } from '@angular/platform-browser' ;
1111import { 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
0 commit comments