Rya*_*ery 10 sorting random angularjs
您如何以随机顺序订购AngularJS中的项目列表?我认为内置orderBy
过滤器可以工作,但我不确定如何不向模型添加一些额外的数据.类似的东西会很棒.
item in items | orderBy:random
Run Code Online (Sandbox Code Playgroud)
我的下一个想法是创建一个自定义过滤器,但如果有更好的东西,我宁愿避免这种情况.
Dan*_*Dan 17
orderBy
可以使用函数参数,就像array.sort
这样你可以使用上面的HTML并random
在范围上定义一个函数,如:
$scope.random = function(){
return 0.5 - Math.random();
};
Run Code Online (Sandbox Code Playgroud)
这将返回一个随机值,有时为负值,有时为正值,有时为0,这将随机排序数组.
小智 6
做一个快速小提琴sh0ber方法似乎运作良好:http: //jsfiddle.net/owenmead/fa4v8/1/
<div ng-controller="MyCtrl">
<p ng-repeat="i in list|orderBy:random">{{i}}</p>
</div>
function MyCtrl($scope) {
$scope.list = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
$scope.random = function() {
return 0.5 - Math.random();
}
}
Run Code Online (Sandbox Code Playgroud)
Angular的orderBy在列表的副本上使用JavaScript的sort().从另一个答案来看,某些浏览器在其类型中是稳定的,而其他浏览器则不是.也许只是在几个浏览器中测试小提琴,你应该好好去: Array.sort在不同的浏览器中排序稳定性
PS.无法评论sh0ber的答案,因为我没有50个代表
归档时间: |
|
查看次数: |
12264 次 |
最近记录: |