使用AngularJS我怎么会随机收集的顺序? [英] Using AngularJS how could I randomize the order of a collection?

查看:122
本文介绍了使用AngularJS我怎么会随机收集的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您将如何订购项目按随机顺序AngularJS列表?我在想,内置排序依据过滤器的工作,但我不知道不增加一些额外的数据模型如何。喜欢的东西将是巨大的。

How would you order a list of items in AngularJS in random order? I was thinking that the built-in orderBy filter would work but I'm not sure how without adding some additional data to the model. Something like would be great.

item in items | orderBy:random

我的下一个想法是创建一个自定义过滤器,但我想preFER避免,如果有更好的东西已经存在。

My next thought was to create a custom filter but I'd prefer to avoid that if there is something better already available.

推荐答案

排序依据可以把一个函数的参数,就像中的Array.sort 所以你可以使用你的HTML上方和定义一个函数随机像范围:

orderBy can take a function parameter, just like array.sort so you can use your HTML above and define a function random on the scope like:

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

这将返回一个随机值有时甚至是负,时而积极,时而0,这将数组排序随机

This will return a random value sometimes negative, sometimes positive, sometimes 0, which will randomly sort the array.

这篇关于使用AngularJS我怎么会随机收集的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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