2017-10-07 22 views
0

我遇到了会话存储中显示名为“currentSet”的JSON对象的问题。当我使用文件夹中的JSON文件时,一切正常,这意味着HTML代码可能没问题。我已经将问题缩小到$ scope.set或loadQuiz-function,并尝试了几件事情,但无法让它工作。这是从控制器相关部分:无法在AngularJS中显示来自会话存储的JSON对象

$scope.set = angular.fromJson(sessionStorage.getItem('currentSet')); //doesn't work 
    //$scope.set = 'data/konflikter.js'; //works 

    $scope.defaultConfig = { 
     'autoMove': true, 
     'pageSize': 1, 
     'showPager': false 
    } 

    $scope.onSelect = function (question, choice) { 
      question.choices.forEach(function (element, index, array) { 
      question.Answered = choice; 
      }); 

     if ($scope.defaultConfig.autoMove == true && $scope.currentPage < $scope.totalItems) { 
      $scope.currentPage++; 
     } 
     else { 
      $scope.onSubmit(); 
     } 
    } 

    $scope.onSubmit = function() { 
     var answers = []; 
     $scope.questions.forEach(function (question, index) { 
      answers.push({'questionid': question._id, 'answer': question.Answered}); 
     }); 
     $http.post('https://fhsclassroom.mybluemix.net/api/quiz/submit', answers).success(function (data, status) { 
      $location.path('/'); 
     }); 
    } 

    $scope.pageCount = function() { 
     return Math.ceil($scope.questions.length/$scope.itemsPerPage); 
    }; 

    $scope.loadQuiz = function (data) { 
     $http.get(data) 
     .then(function (res) { 
      $scope.name = res.data.name; 
      $scope.questions = res.data.questions; 
      $scope.totalItems = $scope.questions.length; 
      $scope.itemsPerPage = $scope.defaultConfig.pageSize; 
      $scope.currentPage = 1; 

      $scope.$watch('currentPage + itemsPerPage', function() { 
       var begin = (($scope.currentPage - 1) * $scope.itemsPerPage), 
        end = begin + $scope.itemsPerPage; 

       $scope.filteredQuestions = $scope.questions.slice(begin, end); 
      }); 
     }); 
    } 
    $scope.loadQuiz($scope.set); 
+0

你可以检查你的会话存储的样子通过只输入控制台:sessionStorage的 – pegla

+0

@pegla是的,我曾与萤火虫检查,我的会话存储包含了我想要的数据,这是一样的'data/konflikter.js'- – danielo

回答

0

好吧,我想通了,什么是错的,你加载文件,所以你需要获取文件 - 或者导入它得到是这样的内容为什么你的作品:

$scope.set = 'data/konflikter.js'; //works but combined with http request 

如果您希望从数据存储传递数据,你需要改变你的loadQuiz不要有这样的HTTP请求:

$scope.loadQuiz = function (res) { 
    $scope.name = res.data.name; 
    $scope.questions = res.data.questions; 
    $scope.totalItems = $scope.questions.length; 
    $scope.itemsPerPage = $scope.defaultConfig.pageSize; 
    $scope.currentPage = 1; 

    $scope.$watch('currentPage + itemsPerPage', function() { 
     var begin = (($scope.currentPage - 1) * $scope.itemsPerPage), 
      end = begin + $scope.itemsPerPage; 

     $scope.filteredQuestions = $scope.questions.slice(begin, end); 
    }); 
} 
+0

中的数据没问题,让我知道它是否有效,如果有帮助,您也可以接受答案并进行投票,与之前所有答案一样,您可以这样做你可能会得到更多的人来帮助你,并在这里获得少量徽章。 – pegla

+0

虽然你在正确的轨道上,但它并不完美。我解决了它,就像我在答案中显示的一样。感谢您的指导! – danielo

0

随着@pegla的帮助,我是能够解决的问题一样这样的:

$scope.loadQuiz = function() { 

     $scope.set = angular.fromJson(sessionStorage.getItem('currentSet')); 

     $scope.questionsetid = $scope.set.questions[0].questionsetid; 
     $scope.name = $scope.set.name; 
     $scope.questions = $scope.set.questions; 
     $scope.totalItems = $scope.set.questions.length; 
     $scope.itemsPerPage = $scope.defaultConfig.pageSize; 
     $scope.currentPage = 1; 

     $scope.$watch('currentPage + itemsPerPage', function() { 
      var begin = (($scope.currentPage - 1) * $scope.itemsPerPage), 
       end = begin + $scope.itemsPerPage; 

      $scope.filteredQuestions = $scope.questions.slice(begin, end); 
     }); 
    } 

    $scope.loadQuiz();