2017-10-07 24 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();