subject/observable working

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

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

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

Loading…
Cancel
Save