Angularjs 如何在Firebase中读取和更新项目$id?
我不知道如何使用基于id的Firebase读取和更新。在这种情况下,我有一个TODO列表,我想根据$id查找第一个项目,然后将Angularjs 如何在Firebase中读取和更新项目$id?,angularjs,firebase,firebase-realtime-database,angularfire,Angularjs,Firebase,Firebase Realtime Database,Angularfire,我不知道如何使用基于id的Firebase读取和更新。在这种情况下,我有一个TODO列表,我想根据$id查找第一个项目,然后将comple更改为true todos -Ke0JWl9xy5sZvT55hfE Complete: false Created_at: 1488228401479 Description: "123" Priority: "high" -Ke0Ms_LnsO-TSwQZLcZaddclose -Ke0
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){
ref.child(todo['$id']).update({ completed: true });
};
return Todo;
}
angular
.module('blocitoff')
.factory('Todo', ['$firebaseArray', Todo]);
})();
这里的
update()
调用使用Firebase JavaScript SDK。但由于AngularFire是建立在这个基础之上的,所以它们的互操作性非常好。谢谢!成功了。这有点相关,所以如何基于$id
返回该对象?当我尝试使用console.log(ref.child(todo['$id'])时,它只返回U{U:Te,path:L,m:ee,Nc:false,然后:undefined…}
。我的目标是获得描述
。该代码只是建立了一个引用。它还没有检索到任何值。要了解如何在不使用AngularFire的情况下从JavaScript访问数据库,我建议:
(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]);
})();