【问题标题】:Can't display JSON-objects from session-storage in AngularJS无法在 AngularJS 中显示会话存储中的 JSON 对象
【发布时间】: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


【解决方案1】:

好的,我知道出了什么问题,您正在加载文件,因此您需要获取文件 - 或导入它以获取内容,这就是它适用于您的内容的原因:

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

如果您希望从 dataStorage 传递数据,您需要将 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);
    });
}

【讨论】:

  • 没问题,如果它有效,请告诉我,如果有帮助,您也可以接受答案并投票,同样您可以对所有以前的答案进行操作,这样您可能会吸引更多人帮助您并在这里获得一些徽章。
  • 虽然你在正确的轨道上,但它并没有完全奏效。我解决了它,就像我在答案中显示的那样。感谢您的指导!
【解决方案2】:

在@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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-02
    • 2017-08-25
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 2017-12-06
    相关资源
    最近更新 更多