-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
116 lines (98 loc) · 1.97 KB
/
app.ts
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
111
112
113
114
115
116
/// <reference path="typings/angular2/angular2.d.ts" />
import {
Component,
NgFor,
View,
bootstrap
}
from "angular2/angular2";
class Article {
title: string;
link: string;
votes: number;
constructor(title, link) {
this.title = title;
this.link = link;
this.votes = 0;
}
voteUp() {
this.votes += 1;
return false;
}
voteDown() {
this.votes -= 1;
return false;
}
domain() {
if (!this.link) {
return '';
}
var link = this.link.split('//')[1];
if (!link) {
return '';
}
return link.split('/')[0];
}
}
@Component({
selector: 'reddit-article',
properties: ['article']
})
@View({
template: `
<article>
<div class="votes">{{ article.votes }}</div>
<div class="main">
<h2>
<a href="{{ article.link }}">{{ article.title }}</a>
<span>({{ article.domain() }})</span>
</h2>
<ul>
<li><a href (click)='article.voteUp()'>upvote</a></li>
<li><a href (click)='article.voteDown()'>downvote</a></li>
</ul>
</div>
</article>
`
})
class RedditArticle {
votes: number;
title: string;
link: string;
}
@Component({
selector: 'reddit'
})
@View({
directives: [RedditArticle, NgFor],
template: `
<section class="new-link">
<div class="control-group">
<div><label for="title">Title:</label></div>
<div><input name="title" #newtitle></div>
</div>
<div class="control-group">
<div><label for="link">Link:</label></div>
<div><input name="link" #newlink></div>
</div>
<button (click)="addArticle(newtitle, newlink)">Submit Link</button>
</section>
<product-row></product-row>
<reddit-article *ng-for="#article of articles" [article]="article"></reddit-article>
`
})
class RedditApp {
articles: Array < article > ;
constructor() {
this.articles = [
new Article('Angular 2', 'http://angular.io'),
new Article('Fullstack', 'http://angular.io')
];
}
addArticle(title, link) {
this.articles.push(new Article(title.value, link.value));
title.value = '';
link.value = '';
}
}
bootstrap(RedditApp);