Skip to content

Instantly share code, notes, and snippets.

@samuelbeek
Created June 18, 2015 09:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save samuelbeek/84721c03607ed5340f53 to your computer and use it in GitHub Desktop.
Save samuelbeek/84721c03607ed5340f53 to your computer and use it in GitHub Desktop.
Random Background Color Directive for Angularjs
// just add random-backgroundcolor to the element you want to give a random background color
app.directive("randomBackgroundcolor", function () {
return {
restrict: 'EA',
replace: false,
link: function (scope, element, attr) {
//generate random color
var color = '#' + (Math.random() * 0xFFFFFF << 0).toString(16);
//Add random background class to selected element
element.css('background-color', color);
}
}
});
@stivenson
Copy link

Genial ...

@qam12
Copy link

qam12 commented Sep 13, 2018

Html File:

<a mat-list-item *ngFor="let Dist of DistributorList; let i = index">
<div " [ngStyle]="{'background': getRandomColor()}">
{{Dist.DistributorTitle | uppercase | slice:0:2}}

TS File:

getRandomColor() {
var color = Math.floor(0x1000000 * Math.random()).toString(16);
return '#' + ('000000' + color).slice(-6);
}
Screen Shot:
radno

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment