subject/observable working

master
Matt Huntington 9 years ago
parent 7cb81edb1c
commit 681229bf67

@ -1,17 +1,17 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable'
const FAKETODOS = [ import { Subject } from 'rxjs/Subject';
'make bed',
'eat',
'drink'
]
@Injectable() @Injectable()
export class TodoService { export class TodoService {
getTodos(){
return FAKETODOS; subject = new Subject();
getTodosObservable(){
return this.subject.asObservable();
} }
addTodo(todo){ addTodo(todo){
FAKETODOS.push(todo); this.subject.next(todo);
} }
} }

@ -12,10 +12,12 @@ export class TodosIndexComponent implements OnInit {
private todoService: TodoService private todoService: TodoService
) { } ) { }
todos; todos = [];
ngOnInit() { ngOnInit() {
this.todos = this.todoService.getTodos(); this.todoService.getTodosObservable().subscribe(newTodo => {
this.todos.push(newTodo);
});
} }
} }

Loading…
Cancel
Save