hello guys i'm trying to implement Upvote feature but i'm having some difficulties to get it toggle, by toggle i mean when user click the button/link it will perform upvote if the user click it again it will remove the upvote, just like a toggle button but i can't seem to find any relevant info from google. this is what i have so far
UPDATED
$scope.up = function(){
if(s_session != null){ // check user is logged in or not
data.getUserData().success(function(userData){ // get user data
$scope.a = data1.votes.up; // this is the data from database
// is a list of username that who voted this post
$scope.b = userData.publicUsername.display;// this is the current username
if($scope.a.indexOf($scope.b) == -1){ //check this username whether is in
// the list if -1 mean not
$scope.user ={};
$scope.user = userData;
$scope.user.permalink = $routeParams.permalink;
$http.post('/contentHandler/post/vote/up',$scope.user). // perform upvote
success(function(updatedData) {
$scope.votedMsg="VOTED";
$scope.afterVoteUp={'color':'#B3B3B3'}
}).error(function(err){
});
}else{ //else remove upvote
$scope.user ={};
$scope.user = userData;
$scope.user.permalink = $routeParams.permalink;
$scope.votedMsg="";
$scope.afterVoteUp={'color':'#2ecc71'}
$http.post('/contentHandler/post/vote/up/remove',$scope.user).
success(function(Data) {
}).error(function(err){
});
}
});
}else{
$location.path('/login'); //redirect to login if not logged in
}
}
but problem is i can't click the button twice, the first click will perform upvote but the second click still will perform upvote, the problem is because the list of username is not being updated, but if i refresh the page and click it again it will remove the upvote. may i is there any better way to do this ?
$scope.clicked = !$scope.clicked; if($scope.clicked){//dosomething}