你好人我試圖實現Upvote功能,但我有一些困難,讓它切換,切換我的意思是當用戶點擊按鈕/鏈接它將執行upvote如果用戶點擊它再次將刪除upvote,就像一個切換按鈕,但我似乎無法從谷歌找到任何相關的信息。這是我迄今爲止如何切換ng點擊
修訂
$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
}
}
,但問題是我不能點擊按鈕兩次,第一次點擊將執行給予好評,但第二次點擊仍將執行給予好評,問題是因爲用戶名列表沒有被更新,但是如果我刷新頁面並再次單擊它,它將刪除upvote。我可否有更好的方法來做到這一點?
使用標誌變量。像'$ scope.clicked =!$ scope.clicked;如果($ scope.clicked){// dosomething}' – Satpal