Use:
$scope.randomQuestion= $scope.myQuestions[Math.floor(Math.random() * $scope.myQuestions.length)];
And in view:
{{randomQuestion}}
UPDATE:
$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:
So, the actual question was not randomizing rather shuffling the questions:
In your js file:
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);
});
Your HTML:
<div ng-repeat="myQuestion in myQuestions">
<p class="txt">{{myQuestion.question}} </p>
</div>
$scope.myQuestionssame name?