我遇到了會話存儲中顯示名爲「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);
你可以檢查你的會話存儲的樣子通過只輸入控制檯:sessionStorage的 – pegla
@pegla是的,我曾與螢火蟲檢查,我的會話存儲包含了我想要的數據,這是一樣的'data/konflikter.js'- – danielo