I have this following function in my controller:
$scope.add = function(newItem){
if (!(newItem.title && newItem.text)) return;
var sanitized = {
title: newItem.title,
text: newItem.text,
date: Firebase.ServerValue.TIMESTAMP,
};
// $rootScope.currentUser is here bound with $firebaseObject(ref).$bindTo($rootScope, 'currentUser').
$rootScope.currentUser.list.push(sanitized);
// .list is an array: [].
};
And I utilise this in my view like follows:
<form name="newUp">
<input ng-model="newItem.title">
<textarea ng-model="newItem.text"></textarea>
<button ng-click="add(newItem)">Submit</button>
</form>
<div ng-repeat="item in currentUser.list | orderBy:'-date'">
<p>
<span><a href="">{{item.title}}</a> <small> - {{item.date | date:'d MMM yy'}}</small></span><br>
<span ng-if="item.text"><small>{{item.text}}</small></span>
</p>
</div>
However after the submit button is clicked, and the item is saved to the firebase database, it displays on screen as:
{".sv":"timestamp"}
After I refresh the page entirely, it shows the item(s) with the correct timestamp(s). Is there a way to avoid this? Can I avoid using $firebaseArray to fix this?
I am using bower which has pulled down the following versions:
From a quick glance there is a bug in work of $firebaseObject
. It's obvious that it doesn't treat properly value of a special object Firebase.ServerValue.TIMESTAMP
which tells server to put the timestamp on server-side . However this is not the case when adding same object in to $firebaseArray
with a synchronized method $add
.
Perhaps I am missing some kind of flush
method for $firebaseObject
but I can't find it.
In any case you should use $firebaseArray
because your data is of type Array
. And don't forget to use $add
, $save
and $remove
as per docs
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.