.callout.is-helpful
header Angular 2 is currently in Developer Preview
p.
We recommend using
Angular for Dart
for production applications.
p.
These instructions assume that you already have the
Dart SDK
and any tools you like to use with Dart.
If you don't have a favorite editor already, try
WebStorm,
which comes with a Dart plugin.
You can also download
Dart plugins for
other IDEs and editors.
p.
Once you have the Dart SDK and any other tools you want, return here.
//- STEP 1 - Create a project ##########################
.l-main-section
h2#section-install-angular 1. Create a project
p.
Create a new directory, and put a file named
pubspec.yaml
in it.
code-example(language="sh").
> mkdir hello_world
> cd hello_world
> vim pubspec.yaml # Use your favorite editor!
p.
In pubspec.yaml
,
specify the angular2 and browser packages as dependencies,
as well as the angular2 transformer.
Angular 2 is changing rapidly, so provide an exact version:
2.0.0-alpha.28.
code-example(language="yaml" format="linenums").
name: hello_world
version: 0.0.1
dependencies:
angular2: 2.0.0-alpha.28
browser: ^0.10.0
transformers:
- angular2:
entry_points: web/main.dart
p.
In the same directory, run pub get
to install the angular2 and browser packages
(along with the packages they depend on):
code-example(language="sh").
> pub get
//- PENDING: Create template? Link to pub/pubspec docs?
//- STEP 2 - Import Angular ##########################
.l-main-section
h2#section-transpile 2. Import Angular
p.
Still in the same directory, create a web
directory.
Create a file under web
named main.dart
.
code-example(language="sh").
> mkdir web
> vim web/main.dart # Use your favorite editor!
p.
Edit web/main.dart
to import the angular2 library
and two reflection libraries:
code-example(language="dart" format="linenums").
import 'package:angular2/angular2.dart';
import 'package:angular2/src/reflection/reflection.dart' show reflector;
import 'package:angular2/src/reflection/reflection_capabilities.dart'
show ReflectionCapabilities;
//- STEP 3 - Define a component ##########################
.l-main-section
h2#section-angular-create-account 3. Define a component
p.
Update web/main.dart
, adding the following code
after the imports:
code-example(language="dart" format="linenums:6").
@Component(
selector: 'my-app'
)
@View(
template: '<h1>Hello {{ name }}</h1>'
)
class AppComponent {
String name = 'Alice';
}
p.
The code you just added creates a component that
has the tag <my-app>.
The Dart code for an Angular component consists of a class
(the component controller)
that has @Component
and @View
annotations.
.l-sub-section
h3 Annotations
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. This component 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="dart").
@Component(
selector: 'my-app'
)
@View(
template: '<h1>Hello {{ name }}</h1>'
)
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.
A template has access to all the public properties and methods
of its component controller.
The template above binds to a name
property through
the double-mustache syntax ({{ ... }}
).
p.
The component controller assigns "Alice" to the name property. When the
template renders, "Hello Alice" appears instead of
"Hello {{ name }}".
code-example(language="dart").
class AppComponent {
String name = 'Alice';
}
//- STEP 4 - Bootstrap ##########################
.l-main-section
h2#section-transpile 4. Bootstrap
p.
Add the following code to the bottom of web/main.dart
:
code-example(language="dart" format="linenums:16").
main() {
reflector.reflectionCapabilities = new ReflectionCapabilities();
bootstrap(AppComponent);
}
p.
This code adds a main()
function
that calls Angular's bootstrap()
function.
The argument to bootstrap()
is the name of the component class
you defined above.
p.
Setting the value of reflector.reflectionCapabilities
lets your app use the Dart VM's reflection (dart:mirrors)
when running in Dartium.
Reflection is fast in native Dart,
so using it makes sense during development.
Later, when you build a JavaScript version of your app,
the Angular transformer will
convert the reflection-using code to static code,
so your generated JavaScript can be smaller and faster.
//- STEP 5 - Declare the HTML ##########################
.l-main-section
h2#section-angular-create-account 5. Declare the HTML
p.
Create a file named web/index.html
that contains
the following code,
which loads main.dart
and instantiates the my-app component:
code-example(language="html" format="linenums").
<!doctype html>
<html>
<head>
<title>Angular 2 Quickstart</title>
</head>
<body>
<my-app></my-app>
<script type="application/dart" src="main.dart"></script>
<script src="packages/browser/dart.js"></script>
</body>
</html>
//- STEP 6 - Run ##########################
.l-main-section
h2#section-angular-run-app 6. Run your app
p.
You have a few options for running your app.
One is to launch a local HTTP server
and then view the app in
Dartium.
You can use whatever server you like, such as WebStorm's server
or Python's SimpleHTTPServer.
p.
Another option is to build and serve the app using pub serve
,
and then run it by visiting http://localhost:8080 in any modern browser.
Pub serve generates the JavaScript on the fly,
which takes a few seconds when you first visit the page.
p.
Once the app is running,
you should see Hello Alice in your browser window.
//- STEP 7 - Generate JavaScript ##########################
.l-main-section
h2#section-angular-run-app 7. Generate JavaScript
p.
Before you can deploy your app, you need to generate JavaScript files.
Compiling your Dart code to JavaScript is easy:
just run pub build
.
code-example(language="basic").
> pub build
Loading source assets...
Loading angular2 transformers...
INFO: Formatter is being overwritten.
Building hello_world... (4.2s)
[Info from Dart2JS]:
Compiling hello_world|web/main.dart...
[Info from Dart2JS]:
Took 0:00:17.408082 to compile hello_world|web/main.dart.
Built 65 files to "build".
//- REGENERATE THIS OUTPUT - or delete it? - when updating from 2.0.0-alpha.28
p.
The generated JavaScript appears, along with supporting files,
under the build
directory.
p.
When you generate JavaScript for an Angular app,
be sure you use the Angular transformer.
It analyzes your code,
converting reflection-using code to static code
that Dart's build tools can compile to faster, smaller JavaScript.
The highlighted lines in pubspec.yaml
configure the Angular transformer:
code-example(language="yaml" format="linenums").
name: hello_world
version: 0.0.1
dependencies:
angular2: 2.0.0-alpha.28
browser: ^0.10.0
transformers:
- angular2:
entry_points: web/main.dart
p.
The entry_points
entry
identifies the Dart file in your app
that has a main()
function.
For more information, see the
Angular
transformer wiki page.
//- WHAT'S NEXT... ##########################
.l-main-section
h2#section-transpile Great job! Next step...
p.
Follow the developer guide
to continue playing with Angular 2 for Dart.
p.
Or read more about Angular or Dart:
ul
li
Angular resources
li
dartlang.org