I create a forEach loop. I can access data from Firebase with this loop and I can change variable. But I can not save these changes on Firebase. How can I save these changes? Here's my code:
我创建了一个forEach循环。我可以通过此循环访问Firebase中的数据,我可以更改变量。但我无法在Firebase上保存这些更改。如何保存这些更改?这是我的代码:
var posts = $firebaseArray(ref);
posts.$loaded().then(function(item){
item.forEach(function(childSnapshot){
var num = childSnapshot.point-1;
childSnapshot.lastPoint = num;
});
});
2 个解决方案
#1
1
You're using the AngularFire framework, which builds UI bindings on top of Firebase's regular JavaScript SDK. You should only be using it for things that you're binding to the Angular UI elements. For everything else, you're likely better off using Firebase's regular JavaScript SDK.
您正在使用AngularFire框架,该框架在Firebase的常规JavaScript SDK之上构建UI绑定。您应该只将它用于绑定到Angular UI元素的内容。对于其他所有内容,您最好使用Firebase的常规JavaScript SDK。
If I understand your requirement correctly, you're trying to loop over all child nodes in a database location once and modify a property. If so:
如果我正确理解了您的要求,您将尝试遍历数据库位置中的所有子节点并修改属性。如果是这样:
ref.once('value', function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var child = childSnapshot.val();
childSnapshot.ref().update({ lastPoint: child.point - 1 });
});
});
The relevant sections of the Firebase documentation are on reading data once and updating data.
Firebase文档的相关部分是一次读取数据和更新数据。
Since AngularFire is built on top of Firebase's JavaScript SDK, they work perfectly together. So if elsewhere you bind the posts to the scope ($scope.posts = $firebaseArray(ref)
), they will be updated automatically when you update the last point with the above snippet.
由于AngularFire构建于Firebase的JavaScript SDK之上,因此它们可以完美地协同工作。因此,如果在其他地方将帖子绑定到范围($ scope.posts = $ firebaseArray(ref)),当您使用上述代码段更新最后一个点时,它们将自动更新。
#2
0
You can create a service that contains a getter and a setter. It would look something like this;
您可以创建包含getter和setter的服务。它看起来像这样;
angular.module('app').factory("DataHandler",
function() {
var data;
return {
get: function() {
return data;
},
set: function(something) {
data = something;
}
}
}
);
Then in your code you would call:
然后在你的代码中你会打电话:
var posts = $firebaseArray(ref);
posts.$loaded().then(function(item){
item.forEach(function(childSnapshot){
var num = childSnapshot.point-1;
childSnapshot.lastPoint = num;
DataHandler.set(childSnapshot);
});
});
Then wherever/whenever you need to get that data just call:
然后,无论何时/何时需要获取该数据,只需调用:
Datahandler.get();
#1
1
You're using the AngularFire framework, which builds UI bindings on top of Firebase's regular JavaScript SDK. You should only be using it for things that you're binding to the Angular UI elements. For everything else, you're likely better off using Firebase's regular JavaScript SDK.
您正在使用AngularFire框架,该框架在Firebase的常规JavaScript SDK之上构建UI绑定。您应该只将它用于绑定到Angular UI元素的内容。对于其他所有内容,您最好使用Firebase的常规JavaScript SDK。
If I understand your requirement correctly, you're trying to loop over all child nodes in a database location once and modify a property. If so:
如果我正确理解了您的要求,您将尝试遍历数据库位置中的所有子节点并修改属性。如果是这样:
ref.once('value', function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var child = childSnapshot.val();
childSnapshot.ref().update({ lastPoint: child.point - 1 });
});
});
The relevant sections of the Firebase documentation are on reading data once and updating data.
Firebase文档的相关部分是一次读取数据和更新数据。
Since AngularFire is built on top of Firebase's JavaScript SDK, they work perfectly together. So if elsewhere you bind the posts to the scope ($scope.posts = $firebaseArray(ref)
), they will be updated automatically when you update the last point with the above snippet.
由于AngularFire构建于Firebase的JavaScript SDK之上,因此它们可以完美地协同工作。因此,如果在其他地方将帖子绑定到范围($ scope.posts = $ firebaseArray(ref)),当您使用上述代码段更新最后一个点时,它们将自动更新。
#2
0
You can create a service that contains a getter and a setter. It would look something like this;
您可以创建包含getter和setter的服务。它看起来像这样;
angular.module('app').factory("DataHandler",
function() {
var data;
return {
get: function() {
return data;
},
set: function(something) {
data = something;
}
}
}
);
Then in your code you would call:
然后在你的代码中你会打电话:
var posts = $firebaseArray(ref);
posts.$loaded().then(function(item){
item.forEach(function(childSnapshot){
var num = childSnapshot.point-1;
childSnapshot.lastPoint = num;
DataHandler.set(childSnapshot);
});
});
Then wherever/whenever you need to get that data just call:
然后,无论何时/何时需要获取该数据,只需调用:
Datahandler.get();