2017-09-02 36 views
0

我給自己定的值,當用戶點擊投票選擇。它的工作。如何使用localStorage的值過濾器在NG-重複

.then(function(response) { 
    localStorage.setItem('isClicked', 'Yes'); 
    var i = +localStorage.key("nid"); 
    var nidId = 'nid' + localStorage.length; 
    localStorage.setItem(nidId, vm.nid); 
    vm.clickedBefore = true; 
}) 

這是我的HTML範圍:

<div class="card myfunc" ng-repeat="myfunc in myfuncPage.myfuncs" id="{{myfunc.nid}}" > 

    <div class="item item-text-wrap"> 
    <h2 class="item-icon-left"><i class="icon ion-ios-analytics"></i> 
     {{myfunc.title}}</h2> 
    </div> 
    <div class="item item-text-wrap" ng-show="localstorage"> 
     <img ng-src="{{myfunc.field_image.und[0].imgPath}}"/> 
     <p class="custom-class" ng-bind-html='myfunc.body.und[0].value'> 
     </p> 
     <ul ng-repeat="vote in myfunc.advmyfunc_choice"> 
     <li ng-repeat="voteselect in vote"> 
      <div class="row"> 
      <button class="col button button-outline button-dark" ng-click="myfuncPage.sendNid(myfunc);myfuncPage.sendVote(voteselect);showVoted = ! showVoted" >{{voteselect.choice}}</button> 
      <div class="voted-screen" ng-show="showVoted"> 
       <h3>Thanks.</h3> 
      </div>  
      </div> 
     </li> 
     </ul> 

    </div> 

    </div> 

在基本上,我需要記住通過localStorage的div和當頁面刷新,隱藏的選擇的div。

ng-show="showVoted"工作的點擊,但我需要刷新。

這樣做的最佳方法是什麼?謝謝。

回答

0

我不知道你使用的本地存儲模塊,所以我不能具體,但我會寫工廠,處理需要

(function() { 
    var voteFactory = function (localStorage) { 
     return { 
      getVote: function (key) { 
       var isVoted = false; 

       // get local storage item, set voted etc 
       var voted = localStorage.getItem(key); 
       if(voted) { 
        isVoted = voted; 
       } 
       return isVoted; 

      }, 
      setVote: function(key, value) { 
       localStorage.setItem(key,value); 
      }   
     }; 
     }; 
     app.factory('voteFactory', ['localStorage', voteFactory]); 
})();  

然後內的值的檢索和存儲範圍控制器/指令您正在使用,以顯示或隱藏你將有一個功能:

$scope.showVoted = function(key) { 
    return voteFactory.getVote(key); 
} 

然後納克放映=「showVoted(theidentifieryouwantouse)」

它也是麥汁同時提及我會使用ng-if而不是ng-show。爲了更有效,你可以您的投票存儲爲一個數組,而不是個人的價值觀和做一個檢查,看看是否你已經檢索到的所有值,從本地存儲獲取如果不。那麼你的職能將詢問重複調用本地存儲的檢索陣列代替。我也是會建議使用可能在工廠一個承諾,因爲從本地存儲檢索可能會推遲造成一些UI古怪。

希望這是一起的,你在找什麼線路。如果需要,我可以詳細說明。