如何在ng-repeat中生成随机颜色。 我尝试了以下方式,但没有工作。
我收到错误,说:
Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!
$scope.getRandomColor = function(){
return {
borderLeft: '2px solid # '+Math.floor((Math.random()*6)+1)
}
};
<div ng-repeat="customer in customerData" class="col-sm-3">
<div class="contact-box" ng-style="getRandomColor()">
</div>
</div>
答案 0 :(得分:5)
这是一个有效的JSFiddle:
HTML:
$lang = JFactory::getLanguage();
$language_tag = $lang->getTag();
echo($language_tag);
return always en-GB ?
JS:
<div ng-app="myApp" ng-controller="dummy">
<div ng-repeat="customer in customerData" class="col-sm-3">
<div class="contact-box" ng-style="customer.color"> {{customer.name}} got: {{customer.color}}</div>
</div>
</div>
答案 1 :(得分:1)
请尝试使用ng-init
和ng-style
视图:
<div ng-repeat="customer in customerData" class="col-sm-3">
<div class="contact-box" ng-init="customer.color = getRandomColor()" ng-style="customer.color">
</div>
</div>
控制器:
$scope.getRandomColor = function(){
return {
borderLeft: '2px solid # '+ Math.floor(Math.random() * 16777215).toString(16)
}
};