【发布时间】:2015-11-22 03:23:23
【问题描述】:
我有一个测验,想随机化来自我的 JSON 文件的数据,这样每次有人尝试测验时都会看到不同的问题顺序。
我遇到了this example,但无法使用来自我的 JSON 文件的数组来解决问题。见下面的JS:
.controller('QuizController', ['$scope', '$http', function($scope, $http, $state){
$scope.score = 0;
$scope.activeQuestion = -1;
$scope.activeQuestionAnswered = 0;
$scope.percentage= 0;
$http.get('js/quiz_data.json').then(function(quizData){
$scope.myQuestions = quizData.data;
$scope.totalQuestions = $scope.myQuestions.length;
});
$scope.randomSort = function(myQuestion) {
return Math.random();
};
HTML:
ng-repeat="myQuestion in myQuestions|orderBy:randomSort">
如果答案也是随机的,那就太好了... 提前致谢!
【问题讨论】:
-
你能提供一个 JSON 的例子吗?
-
是的,就是这样:{ "question" : "Qual o número total de biomas no Brasil?", "answers" : [ {"id" : 0, "text" : "27" },{“id”:1,“text”:“5”},{“id”:2,“text”:“6”},{“id”:3,“text”:“26”}] , "正确" : 2 },
-
从已经随机排序的
GET返回结果是否可行?喜欢将逻辑推到后端?
标签: arrays json angularjs sorting ionic-framework