如何在firebase中更新字典,同时用angularjs添加时间戳

How to update a dictionary within firebase while also add a time stamp with angularjs?

本文关键字:angularjs 时间戳 添加 字典 firebase 更新      更新时间:2023-09-26

下面的代码更新列表中的firebase字典,并为该更新创建时间戳,但复杂的是,我需要将firebase对象转换为html

中的数组

JSFiddle: http://jsfiddle.net/chrisguzman/b84up41y/

HTML

<section ng-app ="myapp" ng-controller="MyController">
<div ng-repeat= "(id,item) in data">
           <form ng-submit="AddComment(id)">
                <input ng-model="item.comment"></input>
            </form>
</div>
</section>
JAVASCRIPT

angular.module("myapp", ["firebase"])
    .controller('MyController',function MyController($scope, $firebase) {
    var ref = new Firebase("https://helloworldtest.firebaseio.com");
    $scope.data = $firebase(ref);
    $scope.AddComment = function (id) {
        $scope.data[id].DateLastModified = Date.now();
        $scope.data.$save(id)}

});

但是当我添加|orderByPriority将firebase对象转换为数组时,我可以像这样应用过滤器

<form ng-submit="AddComment(id) |orderByPriority">

它不再工作

但是,当我删除下列组件时,它确实可以更新列表中的字典,这是我需要的组件。

$scope.data[id].DateLastModified = Date.now();

根据firebase

orderByPriority过滤器是由AngularFire提供的,用来把$firebase返回的对象转换成一个数组。数组中的对象按优先级排序(在Firebase中定义)。此外,数组中的每个对象都有一个定义在其上的$id属性,它将对应于该对象的键名。

但是,id似乎返回0而不是示例中的id i show

直接传递商品的id:

<div ng-repeat= "item in data|orderByPriority">
           <form ng-submit="AddComment(item.$id)">
                <input ng-model="item.comment"></input>
            </form>
</div>

注意,在ng-repeat中,我只在data中寻找item