使用角度ng重复生成随机颜色

时间:2015-09-01 07:42:58

标签: angularjs

如何在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>

2 个答案:

答案 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">&nbsp; {{customer.name}} got: {{customer.color}}</div>
    </div>
</div>

答案 1 :(得分:1)

请尝试使用ng-initng-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)
        }       
    };