0
我想讓我的問題以隨機順序顯示。我做到了,但它不能正常工作,有時沒有遍歷所有對象和停止等在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
你能解釋一步你只是做一步,其實這部分「%$ scope.allData.length」?順便說一下,它效果很好,所以感謝解決方案。你能幫我多做一件事,我在我的「quizController」 - 倒數計時器中評論過一部分內容。它不能像它那樣工作,你能給我一些關於這方面的建議嗎? – ovesyan19 2015-02-07 18:35:08
@Alik:'%'模運算符返回第二個操作數的除法提醒。這種情況下的效果是,當索引在數組外部增長時,它從零開始迴繞。括號內增加的長度是確保如果您用-1方向調用該函數,該值不會爲負值。當你從'setTimeout'調用'countDowner'時,它將被調用爲常規函數,而不是'$ scope'對象的方法。使用'$ timeout(function(){$ scope.countDowner();},1000);'將其稱爲方法。 – Guffa 2015-02-07 18:48:46
非常感謝,現在都變得清晰了。 – ovesyan19 2015-02-07 18:54:51