2014-10-01 27 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); 
     }; 
    } 
);