Angularjs Firebase:更新$save上的项目时间戳
我正在尝试解决如何更新Firebase项目上的Angularjs Firebase:更新$save上的项目时间戳,angularjs,firebase,angularfire,Angularjs,Firebase,Angularfire,我正在尝试解决如何更新Firebase项目上的updatedAt时间戳 更新后的thing.name和thing.description更新到对象,但我无法确定如何更新时间戳,即使我正在编辑的thing对象中传递它 以下是我当前的代码: HTML <h1>You are editing </h1> <section class="unit"> <form ng-submit="updateThing(selectedThing)"> &
updatedAt
时间戳
更新后的thing.name
和thing.description
更新到对象,但我无法确定如何更新时间戳,即使我正在编辑的thing
对象中传递它
以下是我当前的代码:
HTML
<h1>You are editing </h1>
<section class="unit">
<form ng-submit="updateThing(selectedThing)">
<input type="text" name="name" ng-model="selectedThing.name" value=""><br>
<input type="text" name="name" ng-model="selectedThing.description" value="">
<button type="submit">Submit</button>
</form>
</section>
如果我更新名称
或说明
,则会在Firebase中更新这些值。但在更新时,不会生成新的时间戳,也不会推送到Firebase
知道我做错了什么吗?感谢您的帮助。提前谢谢 这不是$save()的工作方式,它不接受任何参数,您必须更改原来的$scope,然后调用$save()
您可以尝试:
$scope.updateThing = function(thing) {
var d = new Date();
$scope.selectedThing.name = thing.name;
$scope.selectedThing.description = thing.description;
$scope.selectedThing.date = d.toISOString()
$scope.selectedThing.$save();
};
利用内置功能,您可以在保存过程中添加时间戳,而无需太多麻烦
app.factory('SyncWithTimestamp', function($firebaseObject) {
return $firebaseObject.$extend({
toJSON: function() {
return {
name: this.name,
description: this.description,
createdAt: this.createdAt,
// when data is returned to the server, set the updatedAt
// to a new, server-generated timestamp (to avoid client discrepancies)
updatedAt: Firebase.ServerValue.TIMESTAMP
}
}
});
});
现在,您只需正常使用扩展工厂:
app.controller(..., function(SyncWithTimestamp) {
$scope.selectedThing = new SyncWithTimestamp(thingsRef.child(serviceId));
});
而updateThing()
方法是多余的,可能会产生bug。无需将数据从同步对象中复制出来,然后尝试将其保存回同步对象(这不是通过将参数传递到$save()
中来完成的)。您只需使用以下功能:
<form ng-submit="selectedThing.$save()">
所有这些,以及更多关于AngularFire的基础知识,都在本文中以剪报为例进行了介绍。详细了解这些信息会让你省去一些痛苦。这似乎无法奏效。获取此错误:
t.selectedService.$save不是一个函数
。我编辑了代码,我认为您可以通过htmlng submit=“selectedThing.$save()”
实现这一点,因为您的$scope.selectedThing
引用了$firebaseObject
,您只需修改它($scope.updatedAt=d.toISOString()
)然后在上面调用$save
。您可能应该研究一下使用Firebase.ServerValue.TIMESTAMP
btw:使用基于UNIX的时间戳有什么好处吗?重要的区别不在于格式,而在于可靠性。如果您有两个客户端,并且它们的时钟完全不同步,则客户端生成的时间戳将是一种不可靠的机制,例如,按顺序排列数据。使用Firebase.ServerValue.TIMESTAMP时,时间戳将始终在服务器上生成。
<form ng-submit="selectedThing.$save()">