angular-cn/public/docs/dart/latest/guide/user-input.jade

199 lines
6.4 KiB
Plaintext
Raw Normal View History

.l-main-section
p.
Use the event syntax <strong>(<em>eventName</em>)</strong> to
make your application respond to user input.
p.
You can specify the event handler—a method in the component controller—like this:
2015-05-19 12:21:11 -04:00
code-example(language="html").
&lt;input (keyup)="myControllerMethod()"&gt;
p.
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-05-19 12:21:11 -04:00
code-example(language="html").
&lt;input #myname (keyup)&gt;
&lt;p&gt;{{myname.value}}&lt;/p&gt;
p.text-body(ng-non-bindable).
In that example, <code>#myname</code> creates a local variable in the template that
the <code>&lt;p&gt;</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>&lt;p&gt;</code> element to the
input's value property.
p.
Let's do something a little more complex, where the user enters items
that the app adds to a list:
2015-04-22 10:49:21 -04:00
figure.image-display
2015-05-26 09:29:27 -04:00
img(src='/resources/images/examples/user-input-example1.png' alt="Example of Todo App")
.l-main-section
h2#section-create-an-array-property Create a list property
p.
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.
code-example(language="dart").
2015-05-19 12:21:11 -04:00
class TodoList {
List&lt;String&gt; todos =[
'Eat breakfast',
'Walk dog',
'Breathe',
'Learn Angular'
];
2015-05-19 12:21:11 -04:00
addTodo(String todo) {
todos.add(todo);
}
2015-05-19 12:21:11 -04:00
}
.callout.is-helpful
header Production Best Practice
p.
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 -->
.l-main-section
h2#section-display-the-list-of-todos Display the list of todos
p.
Using the <code>*ng-for</code> iterator, create an <code>&lt;li&gt;</code> for each item in the todos list and set
its text to the value.
code-example(language="html").
2015-05-19 12:21:11 -04:00
&lt;ul&gt;
&lt;li *ng-for=&quot;#todo of todos&quot;&gt;
2015-05-19 12:21:11 -04:00
{{ todo }}
&lt;/li&gt;
&lt;/ul&gt;
.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 12:21:11 -04:00
code-example(language="html").
&lt;input #todotext&gt;
p.
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-05-19 12:21:11 -04:00
code-example(language="html").
&lt;button (click)="addTodo(todotext.value)"&gt;Add Todo&lt;/button&gt;
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.
code-example(language="dart").
import 'dart:html';
...
// In the template:
&lt;input #todotext (keyup)="doneTyping(\$event)"&gt;
...
// In the component controller class:
doneTyping(KeyboardEvent event) {
if (event.keyCode == KeyCode.ENTER) {
InputElement e = event.target;
addTodo(e.value);
e.value = null;
}
}
.l-main-section
h2#section-final-code Final code
2015-05-19 12:21:11 -04:00
code-tabs
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 12:21:11 -04:00
@Component(selector: 'todo-list')
2015-05-19 12:21:11 -04:00
@View(
// An alternative to using \$event is to use a raw string instead.
// For example, change "template: '''" to "template: r'''".
template: '''
2015-05-19 12:21:11 -04:00
&lt;ul&gt;
&lt;li *ng-for="#todo of todos"&gt;
2015-05-19 12:21:11 -04:00
{{ todo }}
&lt;/li&gt;
&lt;/ul&gt;
&lt;input #todotext (keyup)="doneTyping(\$event)"&gt;
2015-06-03 16:13:20 -04:00
&lt;button (click)="addTodo(todotext.value)"&gt;Add Todo&lt;/button&gt;
''', directives: const [NgFor])
2015-05-19 12:21:11 -04:00
class TodoList {
List&lt;String&gt; todos = [
'Eat breakfast',
'Walk dog',
'Breathe',
'Learn Angular'
];
2015-05-19 12:21:11 -04:00
addTodo(String todo) {
todos.add(todo);
}
2015-05-19 12:21:11 -04:00
doneTyping(KeyboardEvent event) {
if (event.keyCode == KeyCode.ENTER) {
InputElement e = event.target;
addTodo(e.value);
e.value = null;
}
}
2015-05-19 12:21:11 -04:00
}
code-pane(language="dart" name="web/main.dart" format="linenums").
2015-07-30 20:11:07 -04:00
import 'package:angular2/bootstrap.dart';
import 'package:user_input/todo_list.dart';
2015-05-19 12:21:11 -04:00
main() {
bootstrap(TodoList);
}
code-pane(language="html" name="web/index.html" format="linenums").
2015-05-19 12:21:11 -04:00
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
2015-09-23 21:19:14 -04:00
&lt;title&gt;User Input&lt;/title&gt;
2015-05-19 12:21:11 -04:00
&lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot;&gt;
2015-09-23 21:19:14 -04:00
&lt;script async src=&quot;main.dart&quot; type=&quot;application/dart&quot;&gt;&lt;/script&gt;
&lt;script async src=&quot;packages/browser/dart.js&quot;&gt;&lt;/script&gt;
2015-05-19 12:21:11 -04:00
&lt;/head&gt;
&lt;body&gt;
&lt;todo-list&gt;&lt;/todo-list&gt;
&lt;/body&gt;
&lt;/html&gt;
code-pane(language="yaml" name="pubspec.yaml" format="linenums").
name: user_input
description: User Input example
2015-05-19 12:21:11 -04:00
version: 0.0.1
dependencies:
angular2: 2.0.0-alpha.45
browser: ^0.10.0
transformers:
- angular2:
entry_points: web/main.dart