Atula Atula - 2 years ago 185
Javascript Question

Randomly Color Divs inside ng-repeat with unknown number of Items

I have data(say a list of items) and it is coming from $http.get request. I want to change the color of divs containing the items under ng-repeat div. The number of items are random.

My code


.success(function(data) {
$ = data;
.error(function(err) {


<div ng-repeat="data in data">

What I want

enter image description here

What I have tried

Well I tried to put some colors in $scope and repeated them in template but if I have put 5 color in $scope then it is coloring only 5 div in template(working as expected).
So I want a way where I can repeat the color randomly for any number of items returning from $http request.

Answer Source

You can define if you want some specific colors and then make them repeat.

 $scope.colorCodeArray = [

  <div ng-repeat="data in data">
  <div ng-style="{background: colorCodeArray[$index % colorCodeArray.length]}" >{{data}}</div>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download