Angularjs Angular localStorage阵列在页面刷新时不断重置

Angularjs Angular localStorage阵列在页面刷新时不断重置,angularjs,arrays,Angularjs,Arrays,目标: 我希望localStorage缓存的应答数组在第一次加载时仅由4对象变量问题数组的内容预填充 在随后的页面刷新中,我希望加载修改后的localStorage answers阵列,例如,阵列中可能有更多的对象或拼接对象 此时,每次我刷新页面时,它都会恢复到原来的4对象数组 var questions = [{"id":9,"Index":9,"Answered":"No","Correct":"?"},{"id":10,"Index":10,"Answered":"No","Correc

目标:

我希望localStorage缓存的应答数组在第一次加载时仅由4对象变量问题数组的内容预填充 在随后的页面刷新中,我希望加载修改后的localStorage answers阵列,例如,阵列中可能有更多的对象或拼接对象

此时,每次我刷新页面时,它都会恢复到原来的4对象数组

 var questions = [{"id":9,"Index":9,"Answered":"No","Correct":"?"},{"id":10,"Index":10,"Answered":"No","Correct":"?"},{"id":11,"Index":11,"Answered":"No","Correct":"?"},{"id":12,"Index":12,"Answered":"No","Correct":"?"}];

// storing our array as a string
localStorage.setItem("answers", JSON.stringify(questions));

// retrieving our data and converting it back into an array

var retrievedData = localStorage.getItem("answers");


// Possible Solution  this intial array  gets created every time the page refreshes and needs some logic to determine 
// if it's intial length if not choose existing localstorage answers array in cache             

   $scope.answers = JSON.parse(localStorage.getItem('answers'))




   $scope.addAnswer = function (){
    $scope.answers.push({"id":10,"Index":10,"Answered":"Yes","Correct":"Correct"})
        $scope.newAnswer = ''
    }

   $scope.spliceAnswer = function (){
    $scope.answers.splice(1, 1, {"id":10,"Index":10,"Answered":"Yes","Correct":"InCorrect"})
    $scope.replaceAnswer = ''
  }



   $scope.$watch('answers',function(newValue, oldValue){
    if(newValue!=oldValue){
        localStorage.setItem('answers',JSON.stringify(newValue))
    }
}, true)
HTML


添加
代替
答案:{答案}

我认为问题在于:

if(newValue!=oldValue){
    localStorage.setItem('answers',JSON.stringify(newValue))
}
验证新值和旧值,并将其更改为:

if(newValue && oldValue && newValue!=oldValue){
    localStorage.setItem('answers',JSON.stringify(newValue))
}

你能整理一下你的密码吗?您问了37个问题,但只标记了一个正确答案,为什么?在第4行,不直接分配给本地存储,而是首先检查该keyif(localStorage.getItem(“answers”)==null){$scope.answers=JSON.parse(localStorage.getItem('answers'))的值是否存在。这是否可行?是的,它应该可以工作$scope.size=0;$scope.size=(JSON.parse(localStorage.getItem('answers')).length$scope.$watch('size',function(){if($scope.size=0){localStorage.setItem('answers',JSON.stringify(questions));$scope.answers=JSON.parse(localStorage.getItem('answers'))}else{$scope.answers=JSON.parse(localStorage.getItem('answers'))$scope.size=(JSON.parse(localStorage.getItem('answers')).length;;});我尝试使用$scope.size来控制它,但没有效果
if(newValue && oldValue && newValue!=oldValue){
    localStorage.setItem('answers',JSON.stringify(newValue))
}
              $scope.key = ($cookieStore.get('arraySize')) ? ($cookieStore.get('arraySize')) :0 ;




$scope.$watch('key', function () {
    if($scope.key == 0) {
        localStorage.setItem("answers", JSON.stringify(questions));
    } else{
        $scope.answers = JSON.parse(localStorage.getItem('answers'));
    }
});


          $scope.$watch('answers',function(newValue, oldValue){
                if(newValue!=oldValue){
                    localStorage.setItem('answers',JSON.stringify(newValue))
                    $scope.size = (JSON.parse(localStorage.getItem('answers'))).length;
                    $cookieStore.put( 'arraySize' , $scope.size );

                }
            }, true)

            $scope.arrayLength = $cookieStore.get('arraySize');

            $scope.key = parseInt($cookieStore.get('arraySize'));