Created
June 18, 2015 09:56
-
-
Save samuelbeek/84721c03607ed5340f53 to your computer and use it in GitHub Desktop.
Random Background Color Directive for Angularjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} | |
} | |
}); |
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:
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Genial ...