JSON(AngularJS)中的随机对象

Shuffling objects in JSON ( AngularJS)

我希望我的问题以随机顺序显示。我做到了,但它不能正常工作,有时它不会遍历所有对象并停止等

 $scope.move = function (direction) {
    var position = $scope.allData.indexOf($scope.currentQ);
    $scope.currentQ = $scope.allData[position + direction];
};

$http.get("js/json/questions.json").then(function(response){
    $scope.allData = response.data;
    $scope.currentQ = $scope.allData[Math.floor(Math.random() * response.data.length)];
});

$scope.answerClick = function(index){
    $scope.clicks++;

    if(index === $scope.currentQ.answer){
        $scope.score++;
        $scope.rightwrong = "Right";
        $('.right-wrong').css("color", "green");
    }
    else{
        $scope.rightwrong = "Wrong";
        $('.right-wrong').css("color", "red");
    }

    if($scope.clicks === 4){
        resultService.score = $scope.score;
        $location.path('/finish');
    }

    $scope.move(+1);
};

这是整个应用程序:http://plnkr.co/edit/wTQHOz

move方法中你可以超出数组的范围。如果你在第一个问题之前或最后一个问题之后移动,让它环绕:

$scope.currentQ = $scope.allData[(position + direction + $scope.allData.length) % $scope.allData.length];