var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
});
app.directive('selectable', function(){
var selected;
var unselect = function(element) {
element.removeClass('selected');
}
var select = function(element) {
if (selected){
unselect(selected);
}
selected = element;
element.addClass('selected');
}
return {
link : function(scope, element, attrs){
element.on('click', function(){
select(element);
});
}
}
});
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.2.x" src="http://code.angularjs.org/1.2.5/angular.js" data-semver="1.2.5"></script>
<script src="app.js"></script>
</head>
<body>
<div ng-controller="MainCtrl">
<b>different scope:</b>
<div selectable>1</div>
<div selectable>2</div>
</div>
<b>$rootScope</b>
<div selectable>3</div>
<div selectable>4</div>
</html>
/* Put your css in here */
.selected{
background-color:green;
}