Want to get started developing AngularJS / TypeScript applications using Visual Studio Code? In this blogpost I will show you how to get started in less time than a coffee break.
First let’s start with a video containing all the steps needed to get up and running. Everything I have done in this video will be described below.
npm install --save-dev typescript
npm install --save-dev angular
npm install --save-dev @types/angular
1<!DOCTYPE html>
2<html>
3 <head>
4 <script src="/node_modules/angular/angular.min.js"></script>
5 <script src="/out/superheroes.js"></script>
6 </head>
7
8 <body>
9 <superheros></superheros>
10 </body>
11</html>
1var module = angular.module('superHeroesApp', []);
2
3interface ISuperHero {
4 id: number;
5 name: string;
6}
7
8const SUPERHEROS: ISuperHero[] = [
9 { id: 11, name: 'Mr. Nice' },
10 { id: 12, name: 'Narco' },
11 { id: 13, name: 'Bombasto' },
12 { id: 14, name: 'Celeritas' },
13 { id: 15, name: 'Magneta' },
14 { id: 16, name: 'RubberMan' },
15 { id: 17, name: 'Dynama' },
16 { id: 18, name: 'Dr IQ' },
17 { id: 19, name: 'Magma' },
18 { id: 20, name: 'Tornado' },
19 { id: 21, name: 'Superman'}
20];
21
22class SuperHerosComponentController implements ng.IController {
23
24 public heros: ISuperHero[];
25
26 constructor() {}
27
28 public $onInit () {
29 this.heros = SUPERHEROS;
30 }
31}
32
33class SuperHerosComponent implements ng.IComponentOptions {
34
35 public controller: ng.Injectable<ng.IControllerConstructor>;
36 public controllerAs: string;
37 public template: string;
38
39 constructor() {
40 this.controller = SuperHerosComponentController;
41 this.controllerAs = "$ctrl";
42 this.template = `
43 <ul>
44 <li ng-repeat="hero in $ctrl.heros">{{ hero.name }}</li>
45 </ul>
46 `;
47 }
48}
49
50angular
51 .module("mySuperAwesomeApp", [])
52 .component("superheros", new SuperHerosComponent());
53
54angular.element(document).ready(function() {
55 angular.bootstrap(document, ["mySuperAwesomeApp"]);
56});
1{
2 "compilerOptions": {
3 "module": "commonjs",
4 "target": "es2016",
5 "jsx": "preserve",
6 "sourceMap": true,
7 "outDir": "out"
8 },
9 "exclude": [
10 "node_modules",
11 "**/node_modules/*"
12 ]
13}
Run the build tasks (Ctrl+Shift+B) and select ‘tsc: watch -tsconfig.json’ to automatically transpiled the TypeScript on every file change. Notice that the ‘out’ folder now contains a JavaScript file and superheroes.js.map file.
Start a local server using the Live Server extension mentioned in the prerequisites and your browser should display a list of heroes! Notice that when you make a change in the TypeScript the file gets transpiled and your browser will reflect the changes without user interaction! Great!
Finally, I will show you how you can debug your TypeScript code using breakpoints. Please make sure that the Live Server is still running. Also notice that we need to change the url in the launch.json to reflect this.
Great, you did it… now go and grab a coffee because you deserve it 😄