@@ -90,7 +90,7 @@ include ../../../../_includes/_util-fns
90
90
Since we're going to be writing our Angular 2 code in TypeScript, it makes sense to
91
91
bring in the TypeScript compiler even before we begin upgrading.
92
92
93
- In order to use TypeScript's ES6 module system to `import` and `export` code, we're
93
+ In order to use TypeScript's ES2015 module system to `import` and `export` code, we're
94
94
going to need a JavaScript module loader. Our application doesn't currently
95
95
use one, and is just using plain old `<script>` tags and the global `window` scope
96
96
instead. We'll replace this approach with the
@@ -135,8 +135,9 @@ include ../../../../_includes/_util-fns
135
135
module using `System.import`. This will load and execute the `app/app.js` file.
136
136
137
137
We should also configure the TypeScript compiler so that it can understand our
138
- project. Add a `tsconfig.json` file to the `src` directory. It instructs the
139
- TypeScript compiler how to interpret our source files.
138
+ project. We'll add a `tsconfig.json` file to the `src` directory, just like we did
139
+ in the [Quickstart](../quickstart.html). It instructs the TypeScript compiler how
140
+ to interpret our source files.
140
141
141
142
+ makeJson('upgrade/ts/typescript-conversion/src/tsconfig.json' , null , 'src/tsconfig.json' )
142
143
@@ -165,7 +166,7 @@ include ../../../../_includes/_util-fns
165
166
departure from the previous approach which just relied on things being available
166
167
on the global `window` scope.
167
168
168
- Since TypeScript is a superset of ECMAScript 6 , which in turn is a superset
169
+ Since TypeScript is a superset of ECMAScript 2015 , which in turn is a superset
169
170
of ECMAScript 5, we can simply switch the file extensions from `.js` to `.ts`
170
171
and define the imports and exports. We don't need to make other changes to
171
172
our existing code. Instead we'll introduce type annotations and other new
@@ -377,9 +378,9 @@ include ../../../../_includes/_util-fns
377
378
other features in addition to the imports and exports that we're already using.
378
379
There's a lot of value the language can provide in Angular 1 applications.
379
380
380
- For one thing, TypeScript is a superset of ES6 . Any app that has previously
381
+ For one thing, TypeScript is a superset of ES2015 . Any app that has previously
381
382
been written in ES5 - like the PhoneCat example has - can with TypeScript
382
- start incorporating all of the JavaScript features that are new to ES6 .
383
+ start incorporating all of the JavaScript features that are new to ES2015 .
383
384
These include things like `let`s and `const`s, default function parameters,
384
385
and destructuring assignments.
385
386
@@ -413,7 +414,7 @@ include ../../../../_includes/_util-fns
413
414
easier once we do the upgrade.
414
415
415
416
Angular 1 expects controllers to be constructor functions, and that's what
416
- ES6 /TypeScript classes really are, and that means we can just register a
417
+ ES2015 /TypeScript classes really are, and that means we can just register a
417
418
class as a controller and Angular 1 will happily use it. We also won't
418
419
need to make any changes to our test suite as the external behavior of the
419
420
controllers will not change.
@@ -426,10 +427,10 @@ include ../../../../_includes/_util-fns
426
427
What was previously done in the controller function is now done in the class
427
428
constructor function. The class additionally declares three members: The
428
429
array of phones, the name of the current sort key, and the search query. These
429
- are all things we we're attaching to `this`/`vm` earlier, but that weren't
430
- explicitly declared anywhere. The last one of these isn't actually used in the
431
- TypeScript code since it's only referred to in the template, but for the sake
432
- of clarity we want to define all the members our controller will have.
430
+ are all things we have already been attaching to the controller,
431
+ but that weren't explicitly declared anywhere. The last one of these isn't actually
432
+ used in the TypeScript code since it's only referred to in the template, but for
433
+ the sake of clarity we want to define all the members our controller will have.
433
434
434
435
In the Phone detail controller we'll have two members: One for the phone
435
436
that the user is looking at and another for the URL of the currently displayed image.
@@ -443,6 +444,14 @@ include ../../../../_includes/_util-fns
443
444
:marked
444
445
This makes our controller code look a lot more like Angular 2 already. We're
445
446
all set to actually introduce Angular 2 into the project.
447
+
448
+ If we had any Angular 1 services in the project, those would also be
449
+ a good candidate for converting to classes, since like controllers,
450
+ they're also constructor functions. But we only have the `Phone` factory
451
+ in this project, and that's a bit special since it's an `ngResource`
452
+ factory. So we won't be doing anything to it in the preparation stage,
453
+ but will instead turn it directly into an Angular 2 service in the
454
+ next section.
446
455
447
456
.l-main-section
448
457
:marked
@@ -537,8 +546,9 @@ include ../../../../_includes/_util-fns
537
546
+ makeExample('upgrade/ts/ng2_initial/src/app/app.ts' , 'bootstrap' )
538
547
539
548
:marked
540
- We are now running both Angular 1 and 2 at the same time, though there are
541
- no Angular 2 components in it yet.
549
+ We are now running both Angular 1 and 2 at the same time. That's pretty
550
+ exciting! We're not running any actual Angular 2 components yet though,
551
+ so let's do that next.
542
552
543
553
544
554
:marked
@@ -595,9 +605,10 @@ include ../../../../_includes/_util-fns
595
605
596
606
:marked
597
607
The `@Injectable` decorator will attach some dependency injection metadata
598
- to the class, letting Angular 2 know about its dependencies. This is
599
- a marker decorator we need to use for classes that have no other Angular 2
600
- decorators but still need to have their dependencies injected.
608
+ to the class, letting Angular 2 know about its dependencies. As described
609
+ by our [Dependency Injection Guide](../guide/dependency-injection.html),
610
+ this is a marker decorator we need to use for classes that have no other
611
+ Angular 2 decorators but still need to have their dependencies injected.
601
612
602
613
In its constructor the class expects to get the `Http` service. It will
603
614
be injected to it and it is stored as a private field. The service is then
@@ -806,9 +817,16 @@ include ../../../../_includes/_util-fns
806
817
These pipes do not exist in Angular 2, so we're going to need to do
807
818
the filtering and sorting ourselves. Let's define a couple of pipes that
808
819
get the job done.
809
-
820
+
821
+ .alert.is-helpful
822
+ :marked
823
+ If you want to learn more about how pipes in Angular 2
824
+ work, we have [a whole guide on the subject](../guide/pipes.html)
825
+ available!
826
+
827
+ :marked
810
828
For filtering, we'll have a pipe called `PhoneFilterPipe`. It works like
811
- `filter` in Angular 1 in that it filters a collection of objects,
829
+ the `filter` filter in Angular 1 in that it filters a collection of objects,
812
830
matching properties within the objects. But, as opposed to `filter`,
813
831
this pipe is specialized to filter `Phone` objects and we can use
814
832
type annotations to make this explicit:
@@ -858,7 +876,7 @@ include ../../../../_includes/_util-fns
858
876
859
877
:marked
860
878
This is made possible by the `async` pipe, which we can apply in the template.
861
- I knows how to turn an Observable to the (latest) value it has emitted:
879
+ It knows how to turn an Observable to the (latest) value it has emitted:
862
880
863
881
+ makeExample('upgrade/ts/ng2_components/src/app/phone_list/phone_list.html' , 'list' , 'src/app/phone_list/phone_list.html' )
864
882
0 commit comments