1

I've got a simple upVote/downVote counter. It works great on the downvote, but when you click upvote it appends a 1 onto the end of the original upvote variable. (it Doesn't add, it Appends) but only for the upvote. Why is this and how can I fix it?

<span>
<a ng-click="upCount = upCount + 1" ng-init="upCount=location.upVoteCount" 
class="btn btn-success">{{upCount}}
</a>

<a ng-click="downCount = downCount - 1" ng-init="downCount=location.downVoteCount" 
class="btn btn-danger">{{downCount}}
</a>
</span>

Note: both location.upVoteCount and location.downVoteCount are defined as integers. They are populated using ng-repeat="location in locationList". So their values are not explicitly declared in the controller. Changing the + to a - works just fine, the add function also works if you replace location.upVoteCount (or location.downVoteCount) with a number.

Thanks for your help!

4 Answers 4

3

+ means both "add" and "concatenate" while - means "minus".

Force the variables to operate in addition mode:

upCount = (parseInt(upCount) + 1)
Sign up to request clarification or add additional context in comments.

1 Comment

I think you're on the right track here, but that resets the {{upCount}} value to 1 and then you can no longer continue to upvote. It stays at 1
2

Try this

<a ng-click="upCount = upCount + 1" ng-init="upCount=parseInt(location.upVoteCount)" 
class="btn btn-success">{{upCount}}
</a>

<a ng-click="downCount = downCount - 1" ng-init="downCount=parseInt(location.downVoteCount)" 
class="btn btn-danger">{{downCount}}
</a>

Update

If your location.upVoteCount and location.downVoteCount variables aren't integers/numbers then this might be an issue.

Try parsing then first. See ng-init directive above.

4 Comments

Unfortunately this solution freezes the button, in other words, nothing happens when you upvote it or down vote it.
of what type is $scope.upCount and $scope.downCount in your controller?
Neither upCount or downCount are used in the controller. they are just variables similar to what's used in the Angular example here: docs.angularjs.org/api/ng.directive:ngClick
Then this will have to be an issue with your location.upVoteCount and location.downVoteCount.
1

I'm not sure why you're having a problem unless the location entity has strings on it. I just did a quick fiddle and it works fine. Here's the HTML:

<div ng-app="myApp" ng-controller="myController">
    <span>
<a ng-click="upCount = upCount + 1" href="#" ng-init="upCount=location.upVoteCount">Up: {{upCount}}</a>
<a ng-click="downCount = downCount - 1" href="#" ng-init="downCount=location.downVoteCount">Down: {{downCount}}</a>
</span>
</div>

Here's the JavaScript:

var app = angular.module("myApp", []);

app.controller("myController", function($scope) {
    $scope.location = {
        upVoteCount: 0,
        downVoteCount: 0
    };
});

It works just fine up or down voting. Run it here:

http://jsfiddle.net/jeremylikness/bPQXJ/

5 Comments

That's strange. my location.upVoteCount and location.downVoteCount are both being pulled in using ng-repeat="location in locationList". So their values are not explicitly declare in the controller. Could that be part of the issue?
Should be fine as long as the values on location in locationList are not strings. How is locationList being populated?
Ok I figure it out. I left some bit of old test code in the controller that used the upVoteCount variable. Once I deleted that everything worked just as it should. Thank you!
@JeremyLikness , just quick question. I saw your jsfiddle and it works fine. Just curios, if value for object upVoteCount: "0" String, can we convert the String value to int inside ng-click ? thanks!
There is no int in JavaScript, but you can case to Number
1

I'm not in a position to try it out right now, but how about:

ng-click="upCount = upCount - (-1)"

No + operators to confuse the expression evaluator

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.