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.

3
  • Please post the second issue as a separate question. Commented Jul 5, 2017 at 4:21
  • I created another post for the second issue. Commented Jul 5, 2017 at 4:31
  • Thank you, I provided an answer there Commented Jul 5, 2017 at 4:39

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Nice. I use this method in this post that you mentioned. Thanks. Now works for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.