AngularJS randomly shows data error

Here is my launch code in Plunker

  • The first problem I want to solve is: I want to randomly show data. For my case, I have two questions. One of them: “Do you like Foo”, the other - “Do you like the bar.” When I reload the page, I want these questions to appear randomly. For example, this time it may seem like “you like Foo”. But when I first load the page, you like the Bar.

I created an arbitrary random filter, as shown below, but this will lead to an error: "I do not see the validation symbol when checking the checkbox"

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

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

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

0
source share
1 answer

Instead of randomly selecting questions, you can try shuffling your array before displaying any questions on your page.

This way, the elements of the array will be randomly indexed, and you will not need to run a random function for each question.

Here is a post to help you shuffle an array using JavaScript. In addition, you can also use the Lodash Shuffle function .

+4
source

Source: https://habr.com/ru/post/1269502/


All Articles