Adding Upvote-ability


The project says to format the code like ng-click="upVote(post)". Shouldn't it be ng-click="upVote($index)" here?

I still have an issue when I use $index, though. If I make the fourth posts have more upvotes than the third, when I go to upvote the third post (which is least in the amount of upvotes), the fourth post's upvotes increase. You can see what I'm talking about by upvoting Retrofit water fountain with Gatorade here.

Seems like this page is talking about the same problem. Seems like there's a reason to use post instead of $index.

Edit: Got it. I was over-complicating things. The project says that the function is similar to a past function we created called plusOne, which threw me off a bit. It's simpler than that function.


Now I have a similar issue, but in a different area. <a href="#/suggestion/{{$index}}">Comments</a> is used to get the user to the comment page of a particular suggestion. "Retrofit water fountain with Gatorade" has the least amount of votes, but when you click on its comment link, it jumps to the singing suggestion, which has has the corresponding index. This problem seems like it'd be harder to solve than the previous one..

Edit: Ok, so that was easier than I thought it'd be. I just used the index of the particular post to create the link. The link I mentioned in my previous post helped me come up with this idea.


Here is what I used.

In HomeController.js,

		$scope.upVote= function(index) {  
			$scope.posts[index].upvotes += 1; 

In index.html,

<span class="glyphicon glyphicon-plus-sign" ng-click="upVote($index)"></span> Upvotes: {{post.upvotes}}

The suggested code they provided will not work. $[post] should be $scope.posts[post]. Also, I switched post to index, i.e. Before: $[post] After: $[index] and then in index.html, I used $index.


A plenty of suggestions on how to address this particular issue ->


what did you code to add upvote-ability look like, i am having trouble getting the upvote to work. when i clik on the span it does not add to the upvote count. thank you


Could you share all necessary code snippets?


Hi so did you switch back to (post)? cause if I use ng-click=“upVote(post)” with
$scope.upVote = function(post) {
$scope.posts[post].upvotes += 1;
It still does not work.

If you use ($index) like this:

$scope.upVote = function(index) {
$scope.posts[index].upvotes += 1;

Then when you click on upvote it will distort on the site (so I take it you should use (post), please paste your final code here


I got the solution:

$scope.upVote = function(post) {
post.upvotes += 1;