2016-06-07 101 views
0
  // Check voted 
      var votes = res.data.votes; 

      if(votes.length == 0){$scope.like = true;} 
      votes.forEach(function(vote){ 
       if(vote.userId === auth.profile.user_id) { 
        $scope.liked = true; 
       } else { 
        $scope.like = true; 
       } 
      }); 

我已经写了代码来检查,如果用户已经投票或没有,但我有一个小问题与else语句:使用下面的代码中,$scope.liked工作正常for each循环检查| AngularJS

,但else语句仅适用于第一个。 我如何编辑这个,所以他通过所有的选票,如果没有发现,他显示$scope.like

回答

3

只是在循环之外做什么?

votes.forEach(function(vote){ if(vote.userId === auth.profile.user_id) { $scope.liked = true; } }); $scope.like = !$scope.liked;

+0

令人惊叹,哇,我没有想到这一点。谢谢 – Borni