Chris Chris - 1 year ago 84
AngularJS Question

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

The following code updates a firebase dictionary within a list and also creates a time stamp for that update, but the complication is that I need to turn the firebase object into an array within the html


This works:


<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>



angular.module("myapp", ["firebase"])
.controller('MyController',function MyController($scope, $firebase) {
var ref = new Firebase("");

$ = $firebase(ref);
$scope.AddComment = function (id) {
$[id].DateLastModified =;


But when I add |orderByPriority to turn the firebase object into an array so I can apply filter as such

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

it no longer works

However, it does work to update the dictionary within the list when I remove the following, which is the component I need.

$[id].DateLastModified =;

According to firebase

The orderByPriority filter is provided by AngularFire to convert an object returned by $firebase into an array. The objects in the array are ordered by priority (as defined in Firebase). Additionally, each object in the array will have a$id property defined on it, which will correspond to the key name for that object.

However, the id seems to return 0 instead of the id in the example i show

Answer Source

Just pass the id of the item directly like so:

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

Notice that in ng-repeat I am only looking for item in data. Here is a working jsfiddle.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download