如何根据 Firebase 中的项目 $id 读取和更新?

How to read and update based on item $id in Firebase?

我不知道如何使用基于 id 的 Firebase 读取和更新。在这种情况下,我有一个待办事项列表,我想根据它的 $id 找到第一个项目,然后将 comple 更改为 true.

todos
  -Ke0JWl9xy5sZvT55hfE
       Complete:  false
       Created_at: 1488228401479
       Description: "123"
       Priority: "high"
  -Ke0Ms_LnsO-TSwQZLcZaddclose
  -Ke0MuWCLkX29XWYyPBM
  -Ke0MwKOrjpDm9rj6L28
  -Ke0NCmpM3_dTPtY9jVb

JS

(function() {
  function Todo($firebaseArray) {
    var ref = firebase.database().ref().child("todos");
    var todos = $firebaseArray(ref);

    Todo.todos = todos;
    Todo.updateToDo = function(todo){
      console.log(todos)

      console.log(todos[todo['$id']])
    };
    return Todo;
  }

  angular
    .module('blocitoff')
    .factory('Todo', ['$firebaseArray', Todo]);
})();

我会采用更简单的方法:

(function() {
  function Todo($firebaseArray) {
    var ref = firebase.database().ref().child("todos");
    var todos = $firebaseArray(ref);

    Todo.todos = todos;
    Todo.updateToDo = function(todo){
      ref.child(todo['$id']).update({ completed: true });
    };
    return Todo;
  }

  angular
    .module('blocitoff')
    .factory('Todo', ['$firebaseArray', Todo]);
})();

此处的 update() 调用使用 Firebase JavaScript SDK。但由于 AngularFire 是建立在其之上的,因此它们可以完美地互操作。