AngularJS随机显示数据错误 [英] AngularJS randomly show data bug

查看:55
本文介绍了AngularJS随机显示数据错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在Plunker中运行的代码

  1. 我要解决的第一个问题是:我想随机显示数据.就我而言,我有两个问题.一个是您喜欢Foo",另一个是您喜欢Bar".重新加载页面时,我希望这些问题随机出现.例如,这次可能会首先显示你喜欢Foo"吗?但是,当我重新加载页面时,您喜欢酒吧"会首先显示.

我按如下所示制作了一个自定义随机过滤器,但这会导致以下错误:我在选中复选框时看不到检查符号"

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();
} 

在我的Plunker的第14行中,我在问题中添加了< div ng-repeat ="item | orderBy:random">

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

已更新:根据评论,我还为 post 创建了另一条 post 我的另一个.

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

推荐答案

您可以尝试在数组上显示任何问题之前改组数组,而不是随机选择问题.

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.

此处是帖子,可帮助您使用JavaScript整理数组.另外,您也可以使用 Lodash的随机播放功能.

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

这篇关于AngularJS随机显示数据错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆