[Angular 2] 1. QuickStart with Angular2 + TypeScript
This quickstart shows how to write your Angular components in TypeScript.
To get the benefits of TypeScript, we want to have the type definitions available for the compiler and the editor. TypeScript type definitions are typically published in a repo called DefinitelyTyped. To fetch one of the type definitions to the local directory, we use the tsd package manager.
npm install -g tsd@^0.6.0 tsd install angular2 es6-promise rx rx-lite
Run the TypeScript compiler
First to create two files in root, index.html and app.ts.
npm install -g typescript@^1.5.0-beta tsc --watch -m commonjs -t es5 --emitDecoratorMetadata app.ts
Import Angular
import {Component, View, bootstrap} from 'angular2/angular2';
The above import statement uses ES6 module syntax to import three symbols from the Angular module. The module will load at runtime.
Define a component
@Component({ selector: 'my-app' // Defines the <my-app></my-app> tag }) @View({ template: '<h1>Hello {{ name }}</h1>' // Defines the inline template for the component }) class MyAppComponent{ name: string; constructor() { this.name = "Allen Greater"; } }
'@Component' + '@View' + Class -- name + template + controller.
Bootstrap
bootstrap(MyAppComponent);
Load the component
The last step is to load the module for the my-app
component. To do this, we'll use the System library.
<html> <head> <title>Angular 2 Quickstart</title> <script src="https://github.jspm.io/jmcriffey/bower-traceur-runtime@0.0.87/traceur-runtime.js"></script> <script src="https://jspm.io/system@0.16.js"></script> <script src="https://code.angularjs.org/2.0.0-alpha.28/angular2.dev.js"></script> </head> <body> <!-- The app component created in app.ts --> <my-app></my-app> <script>System.import('app');</script> </body> </html>
Run a local server
npm install -g http-server # Or sudo npm install -g http-server http-server # Creates a server at localhost:8080