【发布时间】:2017-10-07 16:59:23
【问题描述】:
我在显示会话存储中名为“currentSet”的 JSON 对象时遇到问题。当我改为使用文件夹中的 JSON 文件时,一切正常,这意味着 HTML 代码可能没问题。我已将问题缩小到 $scope.set 或 loadQuiz-function 并尝试了几件事,但无法让它工作。这是控制器的相关部分:
$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 是的,我已经用 firebug 进行了检查,我的 session-storage 包含我想要的数据,这与 'data/konflikter.js'-file 中的数据相同
标签: javascript html angularjs json angularjs-scope