Created
June 1, 2015 01:17
-
-
Save theotherzach/be07efb76db4db0f1b1b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// <reference path="typings/angular2/angular2.d.ts" /> | |
| import {Component, View, bootstrap, For, If} from 'angular2/angular2'; | |
| class FriendsService { | |
| names: Array<string>; | |
| constructor() { | |
| this.names = ["Aarav", "Martín", "Shannon", "Ariana", "Kai"]; | |
| } | |
| } | |
| class TodoList { | |
| todos: Array<{ name: string; order: number;}>; | |
| constructor() { | |
| this.todos = [ | |
| { name: "Walk dog", order: 3 }, | |
| { name: "Breathe", order: 1 }, | |
| { name: "Eat breakfast", order: 2 }]; | |
| } | |
| addTodo(name: string, order: number) { | |
| this.todos.push({ name, order }); | |
| } | |
| orderByName() { | |
| this.todos = this.todos.sort((a, b) => { | |
| if (a.name[0] > b.name[0]) { | |
| return 1; | |
| } | |
| if (a.name[0] < b.name[0]) { | |
| return -1 | |
| } | |
| return 0 | |
| }) | |
| this | |
| } | |
| orderByOrder() { | |
| this.todos = this.todos.sort((a, b) => { | |
| if (a.order > b.order) { | |
| return 1; | |
| } | |
| if (a.order < b.order) { | |
| return -1 | |
| } | |
| return 0 | |
| }) | |
| this | |
| } | |
| } | |
| // Annotation section | |
| @Component({ | |
| selector: 'my-app', | |
| injectables: [FriendsService, TodoList] | |
| }) | |
| @View({ | |
| directives: [For, If], | |
| template: ` | |
| <input #name (keyup)> | |
| <p>My name is {{ name.value }}</p> | |
| <p>My friends are: </p> | |
| <ul> | |
| <li *for="#name of names"> | |
| {{ name }} | |
| </li> | |
| </ul> | |
| <p *if="names.length > 3"> | |
| So many friends! | |
| </p> | |
| <span (click)="todoList.orderByName()">Task</span> | |
| <span (click)="todoList.orderByOrder()">Order</span> | |
| <ul> | |
| <li *for="#todo of todoList.todos"> | |
| {{ todo.name }} {{ todo.order }} | |
| </li> | |
| </ul> | |
| <input #todotext> | |
| <input #order> | |
| <button (click)="todoList.addTodo(todotext.value, order.value); todotext.value=null; order.value=null"> | |
| Add Todo | |
| </button> | |
| ` | |
| }) | |
| // Component controller | |
| class MyAppComponent { | |
| name: string; | |
| names: Array<string>; | |
| todoList: TodoList; | |
| constructor(friendsService: FriendsService, todoList: TodoList) { | |
| this.name = 'Alice'; | |
| this.names = friendsService.names; | |
| this.todoList = todoList | |
| } | |
| } | |
| bootstrap(MyAppComponent); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!doctype html> | |
| <html> | |
| <head> | |
| <title>Angular 2 Quickstart</title> | |
| <script src="https://github.jspm.io/jmcriffey/bower-traceur-runtime@0.0.87/traceur-runtime.js"></script> | |
| <script src="https://jspm.io/system@0.16.js"></script> | |
| <script src="bundle/angular2.dev.js"></script> | |
| </head> | |
| <body> | |
| <script> | |
| System.import('app'); | |
| </script> | |
| <my-app></my-app> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment