.callout.is-helpful
header Angular is in developer preview
p.
This quickstart does not reflect the final development process for writing apps with Angular.
The following setup is for those who want to try out Angular while it is in developer preview.
// STEP 1 - Create a project ##########################
.l-main-section
h2#section-create-project 1. Create a project
p.
This quickstart shows how to write your Angular components in TypeScript. You could instead choose
another language such as Dart, ES5, or ES6.
p.
The goal of this quickstart is to write a component in TypeScript that prints a string.
We assume you have already installed Node and npm.
p.
To get started, create a new empty project directory. All the following commands should be run
from this directory.
p.
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.
code-example.
$ npm install -g tsd@^0.6.0
$ tsd install angular2 es6-promise rx rx-lite
p.
Next, create two empty files, index.html
and app.ts
, both at the root of the project:
code-example.
$ touch app.ts index.html
// STEP 2 - Start the TypeScript compiler ##########################
.l-main-section
h2#start-tsc 2. Run the TypeScript compiler
p.
Since the browser doesn't understand TypeScript code, we need to run a compiler to translate
your code to browser-compliant JavaScript as you work. This quickstart uses the TypeScript
compiler in --watch
mode, but it is also possible to do the translation in the browser as files
are loaded, or configure your editor or IDE to do it.
code-example.
$ npm install -g typescript@^1.5.0-beta
$ tsc --watch -m commonjs -t es5 --emitDecoratorMetadata app.ts
.callout.is-helpful
p.
Windows users: if you get an error that an option is unknown, you are probably running
an older version of TypeScript.
See
Stack Overflow: How do I install Typescript
// STEP 3 - Import Angular ##########################
.l-main-section
h2#section-transpile 3. Import Angular
p Inside of app.ts
, import the type definitions from Angular:
code-example.
/// <reference path="typings/angular2/angular2.d.ts" />
p Now your editor should be able to complete the available imports:
code-example.
import {Component, View, bootstrap} from 'angular2/angular2';
p.
The above import statement uses ES6 module syntax to import three symbols from the Angular module.
The module will load at runtime.
// STEP 4 - Create a component ##########################
.l-main-section
h2#section-angular-create-account 4. Define a component
p.
Components structure and represent the UI. This quickstart demonstrates the process of creating a component
that has an HTML tag named <my-app>
.
p.
A component consists of two parts, the component controller
which is an ES6 class, and the decorators which tell Angular
how to place the component into the page.
code-example(language="javascript" format="linenums").
// Annotation section
@Component({
selector: 'my-app'
})
@View({
template: '<h1>Hello {{ name }}</h1>'
})
// Component controller
class MyAppComponent {
name: string;
constructor() {
this.name = 'Alice';
}
}
.l-sub-section
h3 @Component and @View annotations
p.
A component annotation describes details about the component. An annotation can be identified by its at-sign (@
).
p.
The @Component
annotation defines the HTML tag for the component by specifying the component's CSS selector.
p.
The @View
annotation defines the HTML that represents the component. The component you wrote uses an inline template, but you can also have an external template. To use an external template, specify a templateUrl
property and give it the path to the HTML file.
code-example(language="javascript" format="linenums").
@Component({
selector: 'my-app' // Defines the <my-app></my-app> tag
})
@View({
template: '<h1>Hello {{ name }}</h1>' // Defines the inline template for the component
})
p.
The annotations above specify an HTML tag of <my-app>
and a template of <h1>Hello {{ name }}</h1>
.
.l-sub-section
h3 The template and the component controller
p.
The component controller is the backing of the component's template. This component
controller uses TypeScript class
syntax.
code-example(language="javascript" format="linenums").
class MyAppComponent {
name: string;
constructor() {
this.name = 'Alice';
}
}
p.
Templates read from their component controllers. Templates have access to any properties
or functions placed on the component controller.
p.
The template above binds to a name
property through
the double-mustache syntax ({{ ... }}
).
The body of the constructor assigns "Alice" to the name property. When the
template renders, "Hello Alice" appears instead of
"Hello {{ name }}".
// STEP 5 - Bootstrap ##########################
.l-main-section
h2#section-transpile 5. Bootstrap
p.
At the bottom of app.ts
, call the bootstrap()
function
to load your new component into its page:
code-example(language="javaScript").
bootstrap(MyAppComponent);
p.
The bootstrap()
function takes a
component as a parameter, enabling the component
(as well as any child components it contains) to render.
// STEP 6 - Declare the HTML ##########################
.l-main-section
h2#section-angular-create-account 6. Declare the HTML
p.
Inside the head
tag of index.html
,
include the traceur-runtime and the Angular bundle.
Instantiate the my-app
component in the body
.
code-example(language="html" format="linenums").
<!-- index.html -->
<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://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>
</body>
</html>
// STEP 7 - Declare the HTML ##########################
.l-main-section
h2#section-load-component-module 7. Load the component
p.
The last step is to load the module for the my-app
component.
To do this, we'll use the System library.
.l-sub-section
h3 System.js
p.
System is a third-party open-source library that
adds ES6 module loading functionality to browsers.
p.
Add the System.js dependency in the <head>
tag, so that
it looks like:
code-example(language="html" format="linenums").
<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>
p.
Add the following module-loading code:
code-example(language="html" format="linenums").
<my-app></my-app>
<script>System.import('app');</script>
// STEP 8 - Run a local server ##########################
.l-main-section
h2#section-load-component-module 8. Run a local server
p Run a local HTTP server, and view index.html
.
p.
If you don't already have an HTTP server,
you can install one using npm install -g http-server
.
(If that results in an access error, then you might need to use
sudo npm ...
)
For example:
code-example.
# From the directory that contains index.html:
npm install -g http-server # Or sudo npm install -g http-server
http-server # Creates a server at localhost:8080
# In a browser, visit localhost:8080/index.html
// WHAT'S NEXT... ##########################
.l-main-section
h2#section-transpile Great job! We'll have the next steps out soon.