| 
									
										
										
										
											2015-04-22 00:23:36 -07:00
										 |  |  | .l-main-section | 
					
						
							|  |  |  |   p. | 
					
						
							| 
									
										
										
										
											2015-04-23 08:58:53 -07:00
										 |  |  |     Use the event syntax <strong>(<em>eventName</em>)</strong> to | 
					
						
							|  |  |  |     make your application respond to user input. | 
					
						
							| 
									
										
										
										
											2015-04-22 00:23:36 -07:00
										 |  |  |   p. | 
					
						
							| 
									
										
										
										
											2015-04-23 08:58:53 -07:00
										 |  |  |     You can specify the event handler—a method in the component controller—like this: | 
					
						
							| 
									
										
										
										
											2015-04-22 00:23:36 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-19 09:21:11 -07:00
										 |  |  |   code-example(language="html"). | 
					
						
							|  |  |  |     <input (keyup)="myControllerMethod()"> | 
					
						
							| 
									
										
										
										
											2015-04-22 00:23:36 -07:00
										 |  |  |   p. | 
					
						
							| 
									
										
										
										
											2015-04-23 08:58:53 -07:00
										 |  |  |     As in previous examples, you can make element references available to | 
					
						
							|  |  |  |     other parts of the template as a local | 
					
						
							|  |  |  |     variable using the <b>#</b> syntax. | 
					
						
							|  |  |  |     Using <code>#</code> and events, | 
					
						
							|  |  |  |     you can write the old "update text as you type" example: | 
					
						
							|  |  |  |     <!-- PENDING: make sure we use recommended word separation scheme. | 
					
						
							|  |  |  |     my-name doesn't work, but my_name does. Would myName work? --> | 
					
						
							| 
									
										
										
										
											2015-04-22 00:23:36 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-19 09:21:11 -07:00
										 |  |  |   code-example(language="html"). | 
					
						
							|  |  |  |     <input #myname (keyup)> | 
					
						
							|  |  |  |     <p>{{myname.value}}</p> | 
					
						
							| 
									
										
										
										
											2015-04-22 00:23:36 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   p.text-body(ng-non-bindable). | 
					
						
							| 
									
										
										
										
											2015-04-23 08:58:53 -07:00
										 |  |  |     In that example, <code>#myname</code> creates a local variable in the template that | 
					
						
							|  |  |  |     the <code><p></code> element can refer to. | 
					
						
							|  |  |  |     The <code>(keyup)</code> tells Angular to trigger updates when it gets a keyup | 
					
						
							|  |  |  |     event.  And <code>{{myname.value}}</code> binds the text node of the | 
					
						
							|  |  |  |     <code><p></code> element to the | 
					
						
							| 
									
										
										
										
											2015-04-22 00:23:36 -07:00
										 |  |  |     input's value property. | 
					
						
							| 
									
										
										
										
											2015-04-23 08:58:53 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   p. | 
					
						
							|  |  |  |     Let's do something a little more complex, where the user enters items | 
					
						
							|  |  |  |     that the app adds to a list: | 
					
						
							| 
									
										
										
										
											2015-04-22 07:49:21 -07:00
										 |  |  |   figure.image-display | 
					
						
							| 
									
										
										
										
											2015-05-26 06:29:27 -07:00
										 |  |  |     img(src='/resources/images/examples/user-input-example1.png' alt="Example of Todo App") | 
					
						
							| 
									
										
										
										
											2015-04-22 00:23:36 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .l-main-section | 
					
						
							| 
									
										
										
										
											2015-04-23 08:58:53 -07:00
										 |  |  |   h2#section-create-an-array-property Create a list property | 
					
						
							| 
									
										
										
										
											2015-04-22 00:23:36 -07:00
										 |  |  |   p. | 
					
						
							| 
									
										
										
										
											2015-04-23 08:58:53 -07:00
										 |  |  |     With the default files in place, | 
					
						
							|  |  |  |     create a TodoController class to manage interactions with the | 
					
						
							|  |  |  |     list. Inside TodoController, add a list with some initial items. | 
					
						
							|  |  |  |     Then add a method that adds new items | 
					
						
							|  |  |  |     to the list. | 
					
						
							| 
									
										
										
										
											2015-04-22 00:23:36 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-03 12:54:59 -07:00
										 |  |  |   code-example(language="dart"). | 
					
						
							| 
									
										
										
										
											2015-05-19 09:21:11 -07:00
										 |  |  |     class TodoList { | 
					
						
							| 
									
										
										
										
											2015-06-03 12:54:59 -07:00
										 |  |  |       List<String> todos =[ | 
					
						
							|  |  |  |         'Eat breakfast', | 
					
						
							|  |  |  |         'Walk dog', | 
					
						
							|  |  |  |         'Breathe', | 
					
						
							|  |  |  |         'Learn Angular' | 
					
						
							|  |  |  |       ]; | 
					
						
							| 
									
										
										
										
											2015-07-01 17:02:06 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-19 09:21:11 -07:00
										 |  |  |       addTodo(String todo) { | 
					
						
							|  |  |  |         todos.add(todo); | 
					
						
							| 
									
										
										
										
											2015-04-23 08:58:53 -07:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2015-05-19 09:21:11 -07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-04-22 00:23:36 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | .callout.is-helpful | 
					
						
							|  |  |  |   header Production Best Practice | 
					
						
							|  |  |  |   p. | 
					
						
							| 
									
										
										
										
											2015-04-23 08:58:53 -07:00
										 |  |  |       As shown in the previous example, a production application you would | 
					
						
							|  |  |  |       separate the model out into another class | 
					
						
							|  |  |  |       and inject it into <code>TodoController</code>. | 
					
						
							|  |  |  |       We've omitted that here for brevity. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <br><br><!-- PENDING: fix formatting of main sections after callouts --> | 
					
						
							| 
									
										
										
										
											2015-04-22 00:23:36 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | .l-main-section | 
					
						
							|  |  |  |   h2#section-display-the-list-of-todos Display the list of todos | 
					
						
							|  |  |  |   p. | 
					
						
							| 
									
										
										
										
											2015-06-03 12:54:59 -07:00
										 |  |  |     Using the <code>*ng-for</code> iterator, create an <code><li></code> for each item in the todos list and set | 
					
						
							| 
									
										
										
										
											2015-04-22 00:23:36 -07:00
										 |  |  |     its text to the value. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-03 12:54:59 -07:00
										 |  |  |   code-example(language="html"). | 
					
						
							| 
									
										
										
										
											2015-05-19 09:21:11 -07:00
										 |  |  |     <ul> | 
					
						
							| 
									
										
										
										
											2015-06-03 12:54:59 -07:00
										 |  |  |        <li *ng-for="#todo of todos"> | 
					
						
							| 
									
										
										
										
											2015-05-19 09:21:11 -07:00
										 |  |  |          {{ todo }} | 
					
						
							|  |  |  |        </li> | 
					
						
							|  |  |  |     </ul> | 
					
						
							| 
									
										
										
										
											2015-04-22 00:23:36 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | .l-main-section | 
					
						
							|  |  |  |   h2#section-add-todos-to-the-list Add todos to the list via button click | 
					
						
							|  |  |  |   p. | 
					
						
							|  |  |  |     Now, add a text input and a button for users to add items to the list.  As you saw above, you can create a local | 
					
						
							|  |  |  |     variable reference in your template with <code>#varname</code>.  Call it <code>#todotext</code> here. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-19 09:21:11 -07:00
										 |  |  |   code-example(language="html"). | 
					
						
							|  |  |  |     <input #todotext> | 
					
						
							| 
									
										
										
										
											2015-04-22 00:23:36 -07:00
										 |  |  |   p. | 
					
						
							| 
									
										
										
										
											2015-04-23 08:58:53 -07:00
										 |  |  |     Specify the target of the click event binding as your controller's | 
					
						
							|  |  |  |     <code>addTodo()</code> method and pass | 
					
						
							|  |  |  |     it the value. Since you created a reference called <code>todotext</code>, | 
					
						
							|  |  |  |     you can get the value with <code>todotext.value.</code> | 
					
						
							| 
									
										
										
										
											2015-04-22 00:23:36 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-19 09:21:11 -07:00
										 |  |  |   code-example(language="html"). | 
					
						
							|  |  |  |     <button (click)="addTodo(todotext.value)">Add Todo</button> | 
					
						
							| 
									
										
										
										
											2015-04-22 00:23:36 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-23 08:58:53 -07:00
										 |  |  |   p. | 
					
						
							|  |  |  |     To make pressing Enter do something useful, | 
					
						
							|  |  |  |     you can add a keyup event handler to the input field. | 
					
						
							|  |  |  |     This event handler uses APIs defined in | 
					
						
							|  |  |  |     <a href="https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/dart:html">dart:html</a>, | 
					
						
							|  |  |  |     so be sure to import that library. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-03 12:54:59 -07:00
										 |  |  |   code-example(language="dart"). | 
					
						
							|  |  |  |     import 'dart:html'; | 
					
						
							|  |  |  |     ... | 
					
						
							| 
									
										
										
										
											2015-04-22 00:23:36 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-03 12:54:59 -07:00
										 |  |  |     // In the template: | 
					
						
							|  |  |  |     <input #todotext (keyup)="doneTyping(\$event)"> | 
					
						
							|  |  |  |     ... | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // In the component controller class: | 
					
						
							|  |  |  |     doneTyping(KeyboardEvent event) { | 
					
						
							|  |  |  |       if (event.keyCode == KeyCode.ENTER) { | 
					
						
							|  |  |  |         InputElement e = event.target; | 
					
						
							|  |  |  |         addTodo(e.value); | 
					
						
							|  |  |  |         e.value = null; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-04-22 00:23:36 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | .l-main-section | 
					
						
							| 
									
										
										
										
											2015-04-23 08:58:53 -07:00
										 |  |  |   h2#section-final-code Final code | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-19 09:21:11 -07:00
										 |  |  |   code-tabs | 
					
						
							| 
									
										
										
										
											2015-06-03 12:54:59 -07:00
										 |  |  |     code-pane(language="dart" name="lib/todo_list.dart" format="linenums"). | 
					
						
							|  |  |  |       library user_input.todo_list; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       import 'dart:html'; | 
					
						
							|  |  |  |       import 'package:angular2/angular2.dart'; | 
					
						
							| 
									
										
										
										
											2015-05-19 09:21:11 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-03 12:54:59 -07:00
										 |  |  |       @Component(selector: 'todo-list') | 
					
						
							| 
									
										
										
										
											2015-05-19 09:21:11 -07:00
										 |  |  |       @View( | 
					
						
							| 
									
										
										
										
											2015-06-03 12:54:59 -07:00
										 |  |  |           // An alternative to using \$event is to use a raw string instead. | 
					
						
							|  |  |  |           // For example, change "template: '''" to "template: r'''". | 
					
						
							|  |  |  |           template: ''' | 
					
						
							| 
									
										
										
										
											2015-05-19 09:21:11 -07:00
										 |  |  |           <ul> | 
					
						
							| 
									
										
										
										
											2015-06-03 12:54:59 -07:00
										 |  |  |             <li *ng-for="#todo of todos"> | 
					
						
							| 
									
										
										
										
											2015-05-19 09:21:11 -07:00
										 |  |  |               {{ todo }} | 
					
						
							|  |  |  |             </li> | 
					
						
							|  |  |  |           </ul> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-03 12:54:59 -07:00
										 |  |  |           <input #todotext (keyup)="doneTyping(\$event)"> | 
					
						
							| 
									
										
										
										
											2015-06-03 13:13:20 -07:00
										 |  |  |           <button (click)="addTodo(todotext.value)">Add Todo</button> | 
					
						
							| 
									
										
										
										
											2015-06-03 12:54:59 -07:00
										 |  |  |       ''', directives: const [NgFor]) | 
					
						
							| 
									
										
										
										
											2015-05-19 09:21:11 -07:00
										 |  |  |       class TodoList { | 
					
						
							| 
									
										
										
										
											2015-06-03 12:54:59 -07:00
										 |  |  |         List<String> todos = [ | 
					
						
							|  |  |  |           'Eat breakfast', | 
					
						
							|  |  |  |           'Walk dog', | 
					
						
							|  |  |  |           'Breathe', | 
					
						
							|  |  |  |           'Learn Angular' | 
					
						
							|  |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2015-05-19 09:21:11 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         addTodo(String todo) { | 
					
						
							|  |  |  |           todos.add(todo); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-04-23 08:58:53 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-19 09:21:11 -07:00
										 |  |  |         doneTyping(KeyboardEvent event) { | 
					
						
							|  |  |  |           if (event.keyCode == KeyCode.ENTER) { | 
					
						
							|  |  |  |             InputElement e = event.target; | 
					
						
							|  |  |  |             addTodo(e.value); | 
					
						
							|  |  |  |             e.value = null; | 
					
						
							| 
									
										
										
										
											2015-04-23 08:58:53 -07:00
										 |  |  |           } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-05-19 09:21:11 -07:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2015-06-03 12:54:59 -07:00
										 |  |  |     code-pane(language="dart" name="web/main.dart" format="linenums"). | 
					
						
							| 
									
										
										
										
											2015-07-30 17:11:07 -07:00
										 |  |  |       import 'package:angular2/bootstrap.dart'; | 
					
						
							| 
									
										
										
										
											2015-06-03 12:54:59 -07:00
										 |  |  |       import 'package:user_input/todo_list.dart'; | 
					
						
							| 
									
										
										
										
											2015-04-23 08:58:53 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-19 09:21:11 -07:00
										 |  |  |       main() { | 
					
						
							|  |  |  |         bootstrap(TodoList); | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2015-06-03 12:54:59 -07:00
										 |  |  |     code-pane(language="html" name="web/index.html" format="linenums"). | 
					
						
							| 
									
										
										
										
											2015-05-19 09:21:11 -07:00
										 |  |  |       <!DOCTYPE html> | 
					
						
							|  |  |  |       <html> | 
					
						
							|  |  |  |         <head> | 
					
						
							| 
									
										
										
										
											2015-09-23 18:19:14 -07:00
										 |  |  |           <title>User Input</title> | 
					
						
							| 
									
										
										
										
											2015-05-19 09:21:11 -07:00
										 |  |  |           <link rel="stylesheet" href="style.css"> | 
					
						
							| 
									
										
										
										
											2015-09-23 18:19:14 -07:00
										 |  |  |           <script async src="main.dart" type="application/dart"></script> | 
					
						
							|  |  |  |           <script async src="packages/browser/dart.js"></script> | 
					
						
							| 
									
										
										
										
											2015-05-19 09:21:11 -07:00
										 |  |  |         </head> | 
					
						
							|  |  |  |         <body> | 
					
						
							|  |  |  |           <todo-list></todo-list> | 
					
						
							|  |  |  |         </body> | 
					
						
							|  |  |  |       </html> | 
					
						
							|  |  |  |     code-pane(language="yaml" name="pubspec.yaml" format="linenums"). | 
					
						
							|  |  |  |       name: user_input | 
					
						
							| 
									
										
										
										
											2015-06-03 12:54:59 -07:00
										 |  |  |       description: User Input example | 
					
						
							| 
									
										
										
										
											2015-05-19 09:21:11 -07:00
										 |  |  |       version: 0.0.1 | 
					
						
							|  |  |  |       dependencies: | 
					
						
							| 
									
										
										
										
											2015-10-06 13:49:38 -07:00
										 |  |  |         angular2: 2.0.0-alpha.39 | 
					
						
							| 
									
										
										
										
											2015-06-03 12:54:59 -07:00
										 |  |  |         browser: ^0.10.0 | 
					
						
							|  |  |  |       transformers: | 
					
						
							|  |  |  |       - angular2: | 
					
						
							|  |  |  |           entry_points: web/main.dart |