2014-10-01 54 views
0

我想将用户投票存储在cookie中,问题在于ng-repeat内部有一个名为session.upVoteCount的值。但它应该是每个事件列表项的单独值。是否可以单独存储每个upVoteCount,然后再单独检索它们?ng-repeat存储单独的值

<li ng-repeat="session in event.sessions | filter:query | orderBy:sortorder" class="span11"> 
       <div class="row session"> 
        <div class="col-sm-1 well votingWidget"> 
         <div class="votingButton" ng-click="upVoteSession(session)"> 
          <span class="glyphicon glyphicon-arrow-up"></span> 
         </div> 
         <div class="badge badge-inverse"> 
          <div>{{session.upVoteCount}}</div> 
         </div> 
         <div class="votingButton" ng-click="downVoteSession(session)"> 
          <span class="glyphicon glyphicon-arrow-down"></span> 
         </div> 
        </div> 

       </div> 
      </li> 

,并在我的控制器我有这样的:

$scope.upVoteSession = function(session) { 
     session.upVoteCount++;    
    }; 

$scope.downVoteSession = function(session) { 
     session.upVoteCount--; 
    }; 
+0

你得到错误?请提供一个jsfiddle(http://jsfiddle.net/) – 2014-10-01 13:32:57

回答

0

首先,我不推荐使用术语“会议”,而是“票”。但是,这是你的电话。

我在这个例子中简化您的问题

http://plnkr.co/edit/l7tQRbuOtEDJetY5eTsf?p=preview

的Javascript:

function MyCtrl($scope) { 
    $scope.votes = {}; 
    $scope.vote = function(key, val) { 
    $scope.votes[key] = $scope.votes[key] || 0; 
    $scope.votes[key]+= val;    
    }; 
} 

HTML:

<li ng-repeat="no in [1,2,3,4,5]"> 
    {{no}} : {{votes[no]}} <br/> 
    <a href="" ng-click="vote(no, +1)">upvote</a> 
    <a href="" ng-click="vote(no, -1)">downvote</a> 
</li> 
0

嗨,大家好我解决了我自己,我不能把它与JSfiddle一起工作,所以我已经上传了整个事情。点击server.bat和浏览器localhost:8000/eventdetails.html,你会看到它的工作。

https://mega.co.nz/#!1d9yiYiA!zTzdztLAmhVDVYOvvVLINETI2bo_WjxCBteWYm2VUKc

控制器:

eventsApp.controller('EventController', 
    function EventController($scope, $cookieStore, eventData) { 
     $scope.sortorder = 'name'; 
     var ape = eventData.getEvent(); 
     ape.then(function (banana) { 
      $scope.event = banana; 
      angular.forEach(banana.sessions, function (value, key) { 
       var storecookie = ($cookieStore.get(value.name)); 
       if (typeof storecookie !== "undefined") { 
        value.upVoteCount = storecookie; 
       } 
      }); 
     }); 
     $scope.upVoteSession = function (session) { 
      session.upVoteCount++; 
      $cookieStore.put(session.name, session.upVoteCount); 
     }; 

     $scope.downVoteSession = function (session) { 
      session.upVoteCount--; 
      $cookieStore.put(session.name, session.upVoteCount); 
     }; 
    } 
);