Использование:
$scope.randomQuestion= $scope.myQuestions[Math.floor(Math.random() * $scope.myQuestions.length)];
И в виду: {{randomQuestion}}
ОБНОВЛЕНИЕ:
$http.get('quiz_data.json').then(function(quizData){
$scope.myQuestions = quizData.data;
$scope.randomQuestion= $scope.myQuestions[Math.floor(Math.random() * $scope.myQuestions.length)];
});
HTML:
<div ng-repeat="myQuestion in myQuestions">
<p class="txt">{{randomQuestion.question}}</p></div>
FINAL UPDATE:
Таким образом, фактический вопрос не рандомизации, а перетасовки вопросы:
в файле JS:
function shuffleArray(array) {
var m = array.length,
t, i;
// While there remain elements to shuffle
while (m) {
// Pick a remaining element…
i = Math.floor(Math.random() * m--);
// And swap it with the current element.
t = array[m];
array[m] = array[i];
array[i] = t;
}
return array;
}
// access the http service
// quizData object = data from quiz_data.json
$http.get('quiz_data.json').then(function(quizData) {
$scope.myQuestions = quizData.data;
$scope.totalQuestions = $scope.myQuestions.length;
shuffleArray($scope.myQuestions);
});
Ваш HTML:
<div ng-repeat="myQuestion in myQuestions">
<p class="txt">{{myQuestion.question}} </p>
</div>
'$ scope.myQuestions' же имя? – Nirus