javascriptangularjsfirebaseangularfirevote-up-buttons

Value update in ng-repeat with AngularJS and Firebase


I'm trying to implement upvotes into my application when a user posts a comment. I can get the votes to display in my view, but can't figure out how to save them to Firebase.

JS

var commentRef = new Firebase(FIREBASE_URL);
var commentsync = $firebase(commentRef);
$scope.messages = commentsync.$asArray();
$scope.addMessage = function(text, item, user, votes) {
  $scope.messages.$add({text: text, item: $scope.item.link, user: Auth.user.email, votes: 0});
  $scope.newMessageText = '';
};

  $scope.voteUp = function(message) {
    message.votes += 1;
    $scope.messages.$save("votes");
   };

HTML

   <button ng-click="voteUp(message)"></button> ({{message.votes}})

Solution

  • After looking at the Firebase documentation on synchronized arrays at https://www.firebase.com/docs/web/libraries/angular/guide.html#section-arrays, it looks like $scope.messages.$save("votes"); should be $scope.messages.$save(message).