AngularJS - How to generate random value for each ng-repeat iteration

前端 未结 3 2073
[愿得一人]
[愿得一人] 2021-02-14 06:30

I am trying to create random span sized divs(.childBox) of twitter bootstrap using AngularJS.

  
3条回答
  •  青春惊慌失措
    2021-02-14 07:18

    As an alternative to the accepted answer, since you're likely to reuse this function, you can turn it into a filter for convenience:

    angular.module('myApp').filter('randomize', function() {
      return function(input, scope) {
        if (input!=null && input!=undefined && input > 1) {
          return Math.floor((Math.random()*input)+1);
        }  
      }
    });
    

    Then, you can define the max and use it anywhere on your app:

      
    

提交回复
热议问题