javascript - JS vote system {{bindings}} gives null on click? -
the error not show in developer tool guess got data , how read. both {{upvote}} , {{downvote}} starts no value , show null on click. somehow buttons linked? setting each vote each item.
background voting system, separate , down scores (not netted single vote score). scores persist in database.
i have not thought limiting votes per user if have thoughts there, feel free include in reply. thanks!
the js files
$scope.upvote = function () { if ($scope.votetype == "down") { $scope.upvote++; } $scope.upvote++; $scope.votetype = "up"; }; $scope.downvote = function () { if ($scope.votetype == "up") { $scope.downvote++; } $scope.downvote++; $scope.votetype = "down"; };
post saved in $scope.post as:
$scope.post = { title: '', upvote: 0, downvote: 0 };
the button in html such:
<i ng-click="downvote()" class="fa fa-chevron-circle-down fa-2x" ng-class="{true:'downvote', false:''}[vote=='downvote']"></i>
$scope
same controller-wide. doesn't change within upvote
.
angular.module('starter').controller('postctrl', function($scope, post) { $scope.posts = post.all; $scope.upvote = function () { $scope.upvote++; // not upvote property of clicked post ... }; });
you'll want grab post $scope.posts
this:
angular.module('starter').controller('postctrl', function($scope, post) { $scope.posts = post.all; $scope.upvote = function (post) { post.upvote++; ... }; });
pass post
in ng-repeat:
<div class="row" ng-repeat="(postid, post) in posts"> <i ng-click="upvote(post)" ...></i> ... </div>
this way, you'll reference clicked post, instead of properties of $scope
itself.
Comments
Post a Comment