21
21
Create a new directory, and put a file named
22
22
<code >pubspec.yaml</code > in it.
23
23
24
- pre.prettyprint
25
- code.
26
- > mkdir hello_world
27
- > cd hello_world
28
- > vim pubspec.yaml # Use your favorite editor!
24
+ code-example( language ="sh" ) .
25
+ > <span class =" blk" >mkdir hello_world</span >
26
+ > <span class =" blk" >cd hello_world</span >
27
+ > <span class =" blk" >vim pubspec.yaml</span > # Use your favorite editor!
29
28
30
29
p.
31
30
In <code >pubspec.yaml</code >, add the angular2 and browser packages as dependencies.
32
31
Angular 2 is changing rapidly, so specify an exact version:
33
- <b >2.0.0-alpha.23</b >.
34
-
35
- pre.prettyprint.linenums.lang-basic
36
- code.
37
- name: hello_world
38
- version: 0.0.1
39
- dependencies:
40
- angular2: 2.0.0-alpha.23
41
- browser: any
32
+ <b >2.0.0-alpha.25</b >.
33
+
34
+ code-example( language ="yaml" ) .
35
+ name: hello_world
36
+ version: 0.0.1
37
+ dependencies:
38
+ angular2: 2.0.0-alpha.25
39
+ browser: any
42
40
p.
43
41
In the same directory, run <code >pub get</code >
44
42
to install the angular2 and browser packages
45
43
(along with the packages they depend on):
46
44
47
- pre.prettyprint.lang-basic
48
- code.
49
- > pub get
45
+ code-example( language ="sh" ) .
46
+ > <span class =" blk" >pub get</span >
50
47
51
48
// PENDING: Create template? Link to pub/pubspec docs?
52
49
// Is browser really needed?
60
57
Still in the same directory, create a <code >web</code > directory.
61
58
Create a file under <code >web</code > named <code >main.dart</code >.
62
59
63
- pre.prettyprint
64
- code.
65
- > mkdir web
66
- > vim web/main.dart # Use your favorite editor!
60
+ code-example( language ="sh" ) .
61
+ > <span class =" blk" >mkdir web</span >
62
+ > <span class =" blk" >vim web/main.dart</span > # Use your favorite editor!
67
63
68
64
p.
69
65
Edit <code >web/main.dart</code > to import the angular2 library
70
- and (for now) two reflection libraries:
66
+ and two reflection libraries:
71
67
72
- // pre.prettyprint.linenums
73
- pre.prettyprint
74
- code.
75
- import 'package:angular2/angular2.dart';
76
-
77
- // These imports will go away soon:
78
- import 'package:angular2/src/reflection/reflection.dart' show reflector;
79
- import 'package:angular2/src/reflection/reflection_capabilities.dart' show ReflectionCapabilities;
80
-
81
- // [PENDING: add line numbers once we can specify where they start]
68
+ code-example( language ="dart" format ="linenums" ) .
69
+ import 'package:angular2/angular2.dart';
70
+ import 'package:angular2/src/reflection/reflection.dart' show reflector;
71
+ import 'package:angular2/src/reflection/reflection_capabilities.dart' show ReflectionCapabilities;
82
72
83
73
// STEP 3 - Define a component ##########################
84
74
.l-main-section
89
79
Update <code >web/main.dart</code >, adding the following code
90
80
after the imports:
91
81
92
- pre.prettyprint
93
- code.
94
- @Component(
95
- selector: 'my-app'
96
- )
97
- @View(
98
- template: '< h1> Hello {{ name }}< /h1> '
99
- )
100
- class AppComponent {
101
- String name = 'Alice';
102
- }
103
-
104
- // [PENDING: add line numbers once we can specify where they start]
82
+ code-example( language ="dart" format ="linenums:5" ) .
83
+ @Component(
84
+ selector: 'my-app'
85
+ )
86
+ @View(
87
+ template: '< h1> Hello {{ name }}< /h1> '
88
+ )
89
+ class AppComponent {
90
+ String name = 'Alice';
91
+ }
105
92
106
93
p.
107
94
The code you just added creates a component that
125
112
specify a <code >templateUrl</code > property
126
113
and give it the path to the HTML file.
127
114
128
- pre.prettyprint
129
- code.
130
- @Component(
131
- selector: 'my-app'
132
- )
133
- @View(
134
- template: '< h1> Hello {{ name }}< /h1> '
135
- )
136
-
137
- // [PENDING: add line numbers once we can specify where they start]
115
+ code-example( language ="dart" ) .
116
+ @Component(
117
+ selector: 'my-app'
118
+ )
119
+ @View(
120
+ template: '< h1> Hello {{ name }}< /h1> '
121
+ )
138
122
139
123
p.
140
124
The annotations above specify an HTML tag of <code >< ; my-app> ; </code >
154
138
template renders, "Hello Alice" appears instead of
155
139
<span ng-non-bindable >"Hello {{ name }}"</span >.
156
140
157
- pre.prettyprint
158
- code.
159
- class AppComponent {
160
- String name = 'Alice';
161
- }
162
-
163
- // [PENDING: add line numbers once we can specify where they start]
164
-
141
+ code-example( language ="dart" ) .
142
+ class AppComponent {
143
+ String name = 'Alice';
144
+ }
165
145
166
146
// STEP 4 - Bootstrap ##########################
167
147
.l-main-section
170
150
p.
171
151
Add the following code to the bottom of <code >web/main.dart</code >:
172
152
173
- pre.prettyprint
174
- code.
175
- main() {
176
- // Temporarily needed.
177
- reflector.reflectionCapabilities = new ReflectionCapabilities();
178
-
179
- bootstrap(AppComponent);
180
- }
181
-
182
- // [PENDING: add line numbers once we can specify where they start]
153
+ code-example( language ="dart" format ="linenums:15" ) .
154
+ main() {
155
+ reflector.reflectionCapabilities = new ReflectionCapabilities();
156
+ bootstrap(AppComponent);
157
+ }
183
158
184
159
p.
185
160
This code adds a <code >main()</code > function
186
161
that calls Angular's <code >bootstrap()</code > function.
187
162
The argument to <code >bootstrap()</code > is the name of the component class
188
163
you defined above.
189
164
190
- p.
191
- Setting the value of
192
- <code >reflector.reflectionCapabilities</code >
193
- is a temporary workaround
194
- that you can remove once Angular's transformer is available.
165
+ //- Explain why setting reflector.reflectionCapabilities is necessary.
195
166
196
167
197
168
// STEP 5 - Declare the HTML ##########################
@@ -204,53 +175,108 @@ p.
204
175
the following code,
205
176
which loads <code >main.dart</code > and instantiates the my-app component:
206
177
207
- pre.prettyprint.linenums
208
- code.
209
- < !doctype html>
210
- < html>
211
- < head>
212
- < title> Angular 2 Quickstart< /title>
213
- < /head>
214
- < body>
215
- < my-app> < /my-app>
216
-
217
- < script type="application/dart" src="main.dart"> < /script>
218
- < script src="packages/browser/dart.js"> < /script>
219
- < /body>
220
- < /html>
178
+ code-example( language ="html" ) .
179
+ < !doctype html>
180
+ < html>
181
+ < head>
182
+ < title> Angular 2 Quickstart< /title>
183
+ < /head>
184
+ < body>
185
+ < my-app> < /my-app>
186
+
187
+ < script type="application/dart" src="main.dart"> < /script>
188
+ < script src="packages/browser/dart.js"> < /script>
189
+ < /body>
190
+ < /html>
221
191
222
192
// STEP 6 - Build and run ##########################
223
193
.l-main-section
224
194
225
195
h2#section-angular-run-app 6. Build and run your app
226
196
227
197
p.
228
- You have several options for running your app.
229
- The quickest and easiest, for now,
230
- is to open your project in <b >Dart Editor</b >,
231
- right-click <b >web/index.html</b >,
232
- and choose <b >Open in Dartium</b >.
233
- This starts a web server
234
- and opens your app in an experimental browser that contains the Dart VM.
235
-
236
- // TODO: screenshot? embedded app?
198
+ You have a few options for running your app.
199
+ One is to launch a local HTTP server
200
+ and then view the app in
201
+ <a href =" https://www.dartlang.org/tools/dartium/" >Dartium</a >.
202
+ You can use whatever server you like, such as Python's SimpleHTTPServer.
237
203
238
204
p.
239
205
Another option is to build and serve the app using <code >pub serve</code >,
240
- and then run it by visiting <b >http://localhost:8080</b > in a browser.
241
- Generating the JavaScript takes a few seconds when you first visit the page,
242
- and the generated JavaScript is currently large.
243
- The generated JavaScript will be smaller once
244
- Angular's transformer becomes available.
206
+ and then run it by visiting <b >http://localhost:8080</b > in any modern browser.
207
+ Pub serve generates the JavaScript on the fly,
208
+ which takes a few seconds when you first visit the page.
245
209
246
- // [PENDING: Update when transformer is working!]
210
+ p.
211
+ Once the app is running,
212
+ you should see <b >Hello Alice</b > in your browser window.
213
+
214
+
215
+ // STEP 7 - Use the Angular transformer ##########################
216
+ .l-main-section
217
+
218
+ h2#section-angular-run-app 7. Use the Angular transformer
219
+
220
+ p.
221
+ To enable quick turnaround while you're developing an Angular app,
222
+ the framework uses reflection via dart:mirrors.
223
+ Unfortunately, mirror-using Dart code compiles to
224
+ JavaScript code that's large and slow.
225
+ To create more deployable JavaScript, use the Angular transformer.
226
+ The transformer analyzes your code,
227
+ converting reflection to static code
228
+ that Dart's build tools can compile to faster, smaller JavaScript.
229
+
230
+ p.
231
+ To use the Angular transformer,
232
+ first add the highlighted lines to your <code >pubspec.yaml</code >:
233
+
234
+ code-example( language ="yaml" ) .
235
+ name: hello_world
236
+ version: 0.0.1
237
+ dependencies:
238
+ angular2: 2.0.0-alpha.25
239
+ browser: any
240
+ <span class =" pnk" >transformers:
241
+ - angular2:
242
+ entry_points: web/main.dart</span >
243
+ p.
244
+ Then build the JavaScript version using <code >pub build</code >:
245
+
246
+ code-example( language ="basic" ) .
247
+ > <span class =" blk" >pub build</span >
248
+ Loading source assets...
249
+ Loading angular2 transformers...
250
+ INFO: Formatter is being overwritten.
251
+ Building hello_world... (3.1s)
252
+ [Info from Dart2JS]:
253
+ Compiling hello_world|web/main.dart...
254
+ [Info from Dart2JS]:
255
+ Took 0:00:16.123086 to compile hello_world|web/main.dart.
256
+ Built 41 files to "build".
257
+
258
+ p.
259
+ The compiled JavaScript appears, along with supporting files,
260
+ under the <code >build</code > directory.
261
+
262
+ p.
263
+ For more information, see the
264
+ <a href =" https://github.com/angular/angular/wiki/Angular-2-Dart-Transformer" >Angular
265
+ transformer wiki page</a >.
247
266
248
267
// WHAT'S NEXT... ##########################
249
268
.l-main-section
250
269
h2#section-transpile Great job! Next step...
251
270
252
271
p.
253
- We plan to add a developer guide soon.
254
- Until then, check out <a href =" resources.html" >Angular Resources</a >.
255
- To learn more about Dart, go to
256
- <a href =" https://www.dartlang.org" >dartlang.org</a >.
272
+ Follow the <a href =" guide" >developer guide</a >
273
+ to continue playing with Angular 2 for Dart.
274
+
275
+ p.
276
+ Or read more about Angular or Dart:
277
+
278
+ ul
279
+ li
280
+ <a href =" resources.html" >Angular resources</a >
281
+ li
282
+ <a href =" https://www.dartlang.org" >dartlang.org</a >
0 commit comments