random orderBy in AngularJS 1.2 returns 'infdig' errors

后端 未结 2 475
别那么骄傲
别那么骄傲 2020-11-29 13:45

Using the random orderBy sort technique in this question works fine in AngularJS 1.1.

var myApp = angular.module(\'myApp\',[]);

function MyCtr         


        
2条回答
  •  旧巷少年郎
    2020-11-29 14:10

    You can solve this in an Angular way with a simple custom filter. Here I'm using the underscore shuffle method which implements Fischer-Yates.

    You could substitute the guts of the shuffle with your own algorithm if you prefer.

    angular.module('shuffle', [])
      .filter('shuffle', function() {
        return function(ary) {
          return _.shuffle(ary);
        }
      });
    

    We can now pipe our array through this filter, like so:

  • The filter will be called once when the template is rendered.

提交回复
热议问题