(function(){
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
function updateValue(){
return {
restrict:'A',
link: function(scope, elem, attrs){
elem.on('click', function(){
scope[attrs.updateValue] = "rgb(" +
getRandomInt(0, 255) + "," +
getRandomInt(0, 255) + "," +
getRandomInt(0, 255) + ")";
scope.$digest();
});
}
};
}
function sillyDomChangeOn(){
return {
restrict:'A',
link: function(scope, elem, attrs){
scope.$watch(attrs.sillyDomChangeOn, function(newVal, oldVal){
elem.css('color', newVal);
});
}
};
}
angular.module('my-app', [])
.directive('updateValue', updateValue)
.directive('sillyDomChangeOn', sillyDomChangeOn);
}());
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css">
<div ng-app="my-app" class="container">
<button type="button" class="btn btn-primary" update-value="randomVal">Update Value</button>
<h3>Value: <code>{{randomVal}}</code></h3>
<div class="well" silly-dom-change-on="randomVal">
<h3>This is just a random DIV</h3>
<div>
</div>