0

I have this function in my Angular controller but I only want the user to be able to add 1 heart, and if they click again, it would remove the heart. How can this be accomplished?

$scope.activeClass = false;

$scope.addHeart = function(post){

    post.hearts += 1;

    $scope.activeClass = !$scope.activeClass;

};

1 Answer 1

1

Try this:

$scope.activeClass = false;

$scope.addHeart = function(post){

    $scope.activeClass ? post.hearts += 1 : post.hearts -= 1;

    $scope.activeClass = !$scope.activeClass;

};
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! I was trying all kinds of crazy if/else statements but now looking at this I fee like an idiot.

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.