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

前端 未结 3 1889
挽巷
挽巷 2021-02-14 06:35

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

  
3条回答
  •  北恋
    北恋 (楼主)
    2021-02-14 07:07

    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:

      
    

提交回复
热议问题