0

Here is my running code in Plunker

  1. First issue I want to solve is: I want to randomly show the data. For my case, I have two questions. One is "Do you like Foo", another one is "Do you like Bar". When I reload the page, I want these questions to show up randomly. For example, maybe "Do you like Foo" show first this time. But when I reload the page, "Do you like Bar" show first.

I made a custom random filter as below, but this will cause bug that "I cannot see the check symbol when I check the checkBox"

$scope.random = function () {
     return 0.5 - Math.random();
} 

Another in my line 14 of my Plunker, I added <div ng-repeat="item in questions | orderBy: random">

Updated: Based on comment, I created another post for my another.

FullStackDeveloper
  • 910
  • 1
  • 16
  • 40

1 Answers1

4

Instead of picking up questions randomly, you can try shuffling your array before showing any question on your page.

This way, the array's elements will be randomly indexed, and you won't need to run the random function for each question.

Here is a post to help you with shuffling the array using JavaScript. Alternatively, you can also use Lodash's Shuffle function.

31piy
  • 23,323
  • 6
  • 47
  • 67