import { bootstrap, onChange, NgIf, NgFor, Component, Directive, View, Ancestor, NgValidator, forwardRef, Binding } from 'angular2/bootstrap'; import {Injectable} from 'angular2/di'; import {formDirectives} from 'angular2/forms'; import {RegExpWrapper, print, isPresent, CONST_EXPR} from 'angular2/src/facade/lang'; /** * You can find the Angular 1 implementation of this example here: * https://github.com/wardbell/ng1DataBinding */ // ---- model var _nextId = 1; class Person { personId: number; mom: Person; dad: Person; friends: Person[]; constructor(public firstName: string, public lastName: string, public yearOfBirth: number) { this.personId = _nextId++; this.firstName = firstName; this.lastName = lastName; this.mom = null; this.dad = null; this.friends = []; this.personId = _nextId++; } get age(): number { return 2015 - this.yearOfBirth; } get fullName(): string { return `${this.firstName} ${this.lastName}`; } get friendNames(): string { return this.friends.map(f => f.fullName).join(', '); } } // ---- services @Injectable() class DataService { currentPerson: Person; persons: Person[]; constructor() { this.persons = [ new Person('Victor', 'Savkin', 1930), new Person('Igor', 'Minar', 1920), new Person('John', 'Papa', 1910), new Person('Nancy', 'Duarte', 1910), new Person('Jack', 'Papa', 1910), new Person('Jill', 'Papa', 1910), new Person('Ward', 'Bell', 1910), new Person('Robert', 'Bell', 1910), new Person('Tracy', 'Ward', 1910), new Person('Dan', 'Wahlin', 1910) ]; this.persons[0].friends = [0, 1, 2, 6, 9].map(_ => this.persons[_]); this.persons[1].friends = [0, 2, 6, 9].map(_ => this.persons[_]); this.persons[2].friends = [0, 1, 6, 9].map(_ => this.persons[_]); this.persons[6].friends = [0, 1, 2, 9].map(_ => this.persons[_]); this.persons[9].friends = [0, 1, 2, 6].map(_ => this.persons[_]); this.persons[2].mom = this.persons[5]; this.persons[2].dad = this.persons[4]; this.persons[6].mom = this.persons[8]; this.persons[6].dad = this.persons[7]; this.currentPerson = this.persons[0]; } } // ---- components @Component({selector: 'full-name-cmp'}) @View({ template: `

Edit Full Name

`, directives: [formDirectives] }) class FullNameComponent { constructor(private service: DataService) {} get person(): Person { return this.service.currentPerson; } } @Component({selector: 'person-detail-cmp'}) @View({ template: `

{{person.fullName}}

Age: {{person.age}}
\
{{person.mom.fullName}}
{{person.dad.fullName}}
{{person.friendNames}}
`, directives: [formDirectives, NgIf] }) class PersonsDetailComponent { constructor(private service: DataService) {} get person(): Person { return this.service.currentPerson; } } @Component({selector: 'persons-cmp'}) @View({ template: `

FullName Demo

`, directives: [formDirectives, PersonsDetailComponent, NgFor] }) class PersonsComponent { persons: Person[]; constructor(private service: DataService) { this.persons = service.persons; } select(person: Person): void { this.service.currentPerson = person; } } @Component({selector: 'person-management-app', viewInjector: [DataService]}) @View({ template: ` `, directives: [FullNameComponent, PersonsComponent, NgIf] }) class PersonManagementApplication { mode: string; switchToEditName(): void { this.mode = 'editName'; } switchToPersonList(): void { this.mode = 'personList'; } } export function main() { bootstrap(PersonManagementApplication); }