<!DOCTYPE html>
<html>
<head>
<title>angular2 playground</title>
<link rel="stylesheet" href="style.css" />
<script src="https://code.angularjs.org/tools/traceur-runtime.js"></script>
<script src="https://code.angularjs.org/tools/system.js"></script>
<script src="https://code.angularjs.org/tools/typescript.js"></script>
<script src="config.js"></script>
<script src="https://code.angularjs.org/2.0.0-alpha.52/angular2.min.js"></script>
<script src="https://code.angularjs.org/2.0.0-alpha.52/http.min.js"></script>
<script>
System.import('app')
.catch(console.error.bind(console));
</script>
</head>
<body>
<app>
loading...
</app>
</body>
</html>
System.config({
//use typescript for compilation
transpiler: 'typescript',
//typescript compiler options
typescriptOptions: {
emitDecoratorMetadata: true
},
//map tells the System loader where to look for things
map: {
app: "./src"
},
//packages defines our app package
packages: {
app: {
main: './main.ts',
defaultExtension: 'ts'
}
}
});
//main entry point
import {bootstrap, View, Component} from 'angular2/angular2';
import {Navbar} from './navbar';
@Component({
selector: "app"
})
@View({
directives: [Navbar]
template: `
<navbar></navbar>
`
})
export class App {
constructor() {
}
}
bootstrap(App, [])
.catch(err => console.error(err));
import {Component, View, NgFor} from 'angular2/angular2';
import {Redify} from './directives';
import {LastnameUppercase} from './pipes';
import {PresidentialCandidate} from './services';
@Component({
selector: "navbar",
directives: [NgFor, Redify],
providers: [PresidentialCandidate],
pipes: [LastnameUppercase],
styles: [`
li{
color: gray;
}
`],
template: `
<h2>Democratic Party presidential candidates</h2>
<ul>
<li redify *ngFor="#item of democrats; #i = index">{{item | lastnameUppercase}} {{i}}</li>
</ul>
<h2>Republican Party presidential candidates</h2>
<ul>
<li redify *ngFor="#item of republicans; #i = index">{{item | lastnameUppercase}} {{i}}</li>
</ul>
`
})
export class Navbar {
democrats: Array<String>
republicans: Array<String>
constructor(private presidentialService :PresidentialCandidate) {
this.democrats = presidentialService.getDemocraticList();
this.republicans = presidentialService.getRepublicainList();
}
ngOnInit() {
console.log('[Component] navbar ngOnInit');
}
}
import {Directive, ElementRef, Renderer} from 'angular2/angular2';
@Directive({
selector: '[redify]'
})
export class Redify {
constructor(private _element: ElementRef, private renderer: Renderer) {
renderer.setElementStyle(_element, 'color', 'red');
}
}
import {Pipe} from 'angular2/angular2';
@Pipe({
name: 'lastnameUppercase'
})
export class LastnameUppercase {
transform(v, args) {
return `${v.split(' ')[0]} ${v.split(' ')[1].toUpperCase()}`;
}
}
import {Injectable} from 'angular2/angular2';
@Injectable()
export class PresidentialCandidate {
constructor() {
}
getRepublicainList() {
return [
"Donald Trump",
"Rand Paul",
"Ben Carson"
]
}
getDemocraticList() {
return [
"Hillary Clinton",
"Martin O'Malley",
"Bernie Sanders"
]
}
}