更新Firebase

时间:2017-05-26 22:30:06

标签: angular firebase firebase-realtime-database angularfire2

在更新Firebase中的值时,我的项目列表会在几分之一秒内被洗牌。

这是我的服务(todos.service):

...
export class TodosService {
  todos$: FirebaseListObservable<any[]>

  constructor(
     private db: AngularFireDatabase
  ) {
    this.todos$ = db.list('/todos/') as FirebaseListObservable<any>;
  }

   getToDos(filter){
    if (filter == undefined) {
      return this.todos$
    } 
    return this.todos$.map(_todo => _todo.filter(todo => todo.done == filter ));
   }

   addTodo(todo){
      todo.date = firebase.database.ServerValue.TIMESTAMP;
      this.todos$.push(todo);
      console.log('addTodo');
   }

   updateTodo(key, todo){
     this.todos$.update(key, todo);
   }

   deleteTodo(todo){
     this.todos$.remove(todo);
   }

}

以下是我如何获取列表(todos.component):

....
constructor (
    private todosService: TodosService
  ){}

  getItems(){
    this.todosService.getToDos(this.filter)
                  .map(todos => todos.reverse())
                  .subscribe(todos => {
                    this.todos = todos;
                  })
  }

  ngOnInit(){
    this.getItems();
  }

以及如何更新它(new-todo.component):

...
addTodo(){
    let todo = {
      title: this.title,
      done: false
    }
    this.todoService.addTodo(todo);
    this.title = '';
  }
...

以下是发生的事情: enter image description here 向正确方向指出的一点将非常感激。

0 个答案:

没有答案