-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo.html
110 lines (101 loc) · 3.11 KB
/
todo.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>待办事项</title>
<!--模块加载器-->
<script type="text/javascript" src="lib/system@0.16.11.js"></script>
<!--Angular2模块库-->
<script type="text/javascript" src="lib/angular2.dev.js"></script>
<script type="text/javascript" src="system.config.js"></script>
</head>
<body>
<a href="/">返回</a>
<!--组件渲染锚点-->
<todo-app></tudo-app>
<!--定义一个ES6脚本元素-->
<script type="module">
//从模块库引入三个类型定义
import {Component,View,NgFor,bootstrap, EventEmitter} from "angular2/angular2";
import {formDirectives} from "angular2/forms";
@Component({
selector: 'task',
properties: ['task'],
events: ['remove']
})
@View({
directives: [formDirectives],
styles: [`
li a {
margin-left: 10px;
font-size: 0.8em;
}
li.done span {
text-decoration: line-through;
color: green;
font-style: italic;
}
`],
template: `
<li [class.done]="task.done">
<span (click)="toggle()">
<input type="checkbox" [ng-model]="task.done" />
{{ task.title }}
</span>
<a href="javascript:;" (click)="removeClicked()">删除</a>
</li>
`
})
class Task{
constructor() {
this.remove = new EventEmitter();
}
toggle() {
this.task.done = !this.task.done;
}
removeClicked() {
this.remove.next();
}
}
//组件定义
@Component({ selector: "todo-app" })
@View({
directives:[Task, NgFor, formDirectives],
template: `
<h3>待办事项</h3>
<ul>
<task
*ng-for="#task of tasks; #i=index"
[task]="task"
(remove)="removeTask(i)"
></task>
</ul>
<form #f="form"
(submit)="addTask(f.value);"
>
<input type="text" ng-control="title" [(ng-model)]="title" />
<button type="submit">添加</button>
</form>
`
})
class TodoApp{
constructor() {
this.tasks = [
{ title: '吃饭', done: true },
{ title: '工作', done: false }
];
this.title = "";
}
addTask(data) {
this.tasks.push({ title: data.title, done: false });
this.title = "";
}
removeTask(index) {
this.tasks.splice(index, 1);
}
}
//渲染组件
bootstrap(TodoApp);
</script>
</body>
</html>