【问题标题】:Error displaying date using angularfire and firebase使用 angularfire 和 firebase 显示日期时出错
【发布时间】:2016-06-24 23:25:16
【问题描述】:

我无法使用 Angularfire 从 firebase 显示日期(存储为 Unix 时间戳整数)。

我有以下html:

<div class="form-group"">
    <label for="message-followUp" class="control-label">Follow Up Date:</label>
    <input type="date" id="message-followUp" ng-model="postToUpdate.followUp">
</div>

和以下控制器:

 $scope.notePost = function (id) {
    var firebaseObj = new Firebase("https://firebase_url/Records/" + id);
    $scope.postToUpdate = $firebaseObject(firebaseObj);
};

在返回的数据中,我有一个名为“followUp”的键,其中包含 Unix 时间戳。显示 html 时出现以下错误:

错误:[ngModel:datefmt] 预期 1466773470397 是一个日期

我希望我可以将 'postToUpdate.followUp' 包装在 new date() 中,但这不起作用

如何将 Unix 时间戳转换为正确的日期格式 (yyyy-MM-dd),以便在显示时不会出现错误?

谢谢

【问题讨论】:

  • 使用new Date 会发生什么?

标签: angularjs firebase angularfire firebase-realtime-database


【解决方案1】:

您从 firebase 检索的数据是一个字符串,当传递给 new Date 时,inux 时间戳应该是一个整数。

JS

$scope.notePost = function (id) {
    var firebaseObj = new Firebase("https://firebase_url/Records/" + id);
    $scope.postToUpdate = $firebaseObject(firebaseObj);
    $scope.postToUpdate.$loaded(
        function(data) {
            console.log(data);
            $scope.date = new Date(parseInt(data.followUp));
        },
        function(error) {
            console.error("Error:", error);
        }
    );
};

HTML

<div class="form-group"">
    <label for="message-followUp" class="control-label">Follow Up Date:</label>
    <input type="date" id="message-followUp" ng-model="date">
</div>

Working jsFiddle here.

【讨论】:

  • 谢谢,当我使用 $scope.date = new Date($scope.postToUpdate.followUp) 然后 console.log($scope.date) 我得到'无效日期'
  • @Janbango 现在我明白你真正的问题是什么了。更新了答案:)。看看 jsFiddle 看看它的工作原理。
  • 不幸的是仍然无法正常工作。我认为这一定是我试图从firebase对象访问'followUp'键的方式,因为console.log($scope.postToUpdate.followUp)返回'undefined'。
  • @Janbango 再看看。如果还是不行。请设置一个 jsFiddle
  • @Janbango 很高兴你成功了! :)
猜你喜欢
  • 2021-04-22
  • 2019-04-08
  • 2014-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-29
  • 1970-01-01
相关资源
最近更新 更多