我的 randomWidth() 函数调用两次时遇到问题,即使我从元素中删除了 ng-repeat 属性,它仍然调用了两次。我似乎无法弄清楚如何解决这个问题。我猜有办法解决这个问题,或者可能是我明显遗漏的东西?
HTML:
<div id="body-wrapper" ng-app="gallery" ng-controller="mainCtrl">
<section id="sidebar">
<h1>Gallery</h1>
</section>
<main>
<div class="box" ng-repeat="img in imgs" ng-style="{'width': randomWidth()}">
<div class="box-wrapper"></div>
</div>
</main>
</div>
Javascript:
angular.module('gallery', [])
.controller('mainCtrl', function($scope){
$scope.imgs = [
{
title: 'image1'
},
{
title: 'image2'
},
{
title: 'image3'
},
{
title: 'image4'
},
{
title: 'image5'
},
{
title: 'image6'
}
];
$scope.randomWidth = function(){
const widths = ['25%', '33%', '40%', '50%'];
const max = widths.length;
const min = 0;
var r = Math.floor(Math.random() * (max - min)) + min;
console.log(widths[r]);
return widths[r];
}
})