Created
March 20, 2016 18:10
-
-
Save zhentian-wan/2672a07fe0c39e88ffd8 to your computer and use it in GitHub Desktop.
Angular 2 Create a TodoModel
This file contains 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
import {Component} from 'angular2/core'; | |
import {bootstrap} from 'angular2/bootstrap'; | |
import {TodoService} from './TodoService'; | |
import {TodoInput} from './TodoInput'; | |
import {TodoList} from './TodoList'; | |
@Component({ | |
selector: 'app', | |
directives: [TodoInput, TodoList], | |
template: ` | |
<todo-input></todo-input> | |
<todo-list></todo-list> | |
` | |
}) | |
class App { | |
} | |
// []: we can inject service which can be used for the whole app | |
bootstrap(App, [ | |
TodoService | |
]); |
This file contains 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
import {Component} from 'angular2/core'; | |
import {TodoService} from './TodoService'; | |
import {TodoModule} from './TodoModule'; | |
@Component({ | |
selector: 'todo-input', | |
template: ` | |
<form (submit)="onSubmit()"> | |
<input type="text" [(ngModel)]="todoModule.title"> | |
</form> | |
` | |
}) | |
export class TodoInput{ | |
// Init a todoModule | |
todoModule:TodoModule = new TodoModule(); | |
constructor(public todoService: TodoService){ | |
} | |
onSubmit(){ | |
this.todoService.todos.push(this.todoModule); | |
// After insert, create new TodoModule | |
this.todoModule = new TodoModule(); | |
console.log(this.todoService.todos); | |
} | |
} |
This file contains 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
import {Component} from 'angular2/core'; | |
import {TodoService} from './TodoService'; | |
@Component({ | |
selector: 'todo-list', | |
template: ` | |
<ul> | |
<li *ngFor="#todo of todoService.todos"> | |
{{todo.title}} | |
</li> | |
</ul> | |
` | |
}) | |
export class TodoList{ | |
constructor(public todoService: TodoService){ | |
} | |
} |
This file contains 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
export class TodoModule{ | |
status: boolean = false; | |
constructor(public title:string=""){ | |
} | |
} |
This file contains 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
import {TodoModule} from './TodoModule'; | |
export class TodoService{ | |
todos = [ | |
//init some todos | |
new TodoModule("eat"), | |
new TodoModule("sleep"), | |
new TodoModule("code") | |
]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment