I'm creating custom directive for all h1-h6 elements. All directives body are the same: they set random color for h-element.
Here is my working code - ugly and DRY
angular.module('example', []);
angular.module('example').factory('randomColor', function () {
return function () {
var colors = ['#FF6A88', '#39FF82', '#7AD5D5', '#DFAF01'];
var randomPick = parseInt(Math.random() * colors.length);
return colors[randomPick];
};
});
angular.module('example').directive('h1',['randomColor', function (randomColor) {
return {
restrict: 'E',
link: function (scope, element) {
$(element).css({ color: randomColor() });
}
};
}]);
angular.module('example').directive('h2',['randomColor', function (randomColor) {
return {
restrict: 'E',
link: function (scope, element) {
$(element).css({ color: randomColor() });
}
};
}]);
//the same for h3-h6
You can check this code here: Plunker - DRY version
Is possible to achieve something like this?
angular.module('example').directive(['h1','h2','h3','h4','h5','h6'],['randomColor', function (randomColor) {
return {
restrict: 'E',
link: function (scope, element) {
$(element).css({ color: randomColor() });
}
};
}]);