Just because you click on a div, it doesn't mean it gets keyboard focus. And if it doesn't get keyboard focus (as it is not editable), it can't lose it and thus can't blur.
An alternative approach could be the following:
Upon clicking on any .cell element, remove the selected class from every element that has it and then add it to the clicked element.
E.g.:
.directive('cell', function cellDirective($window) {
return {
restrict: 'C',
link: function cellPostLink(scope, elem, attrs) {
elem.on('click', onClick);
scope.$on('$destroy', onDestroy);
function onClick() {
Array.prototype.forEach.call(
$window.document.querySelectorAll('.selected'),
function (el) { angular.element(el).removeClass('selected'); });
elem.addClass('selected');
}
function onDestroy() {
elem.off('click', onClick);
});
}
};
});
See, also, this short demo.
NOTES:
Depending on the browsers you are supporting and on whether you are using jQuery or not the code above might need modifications.
An alternative approach would be to register a click-listener on the document and remove the selected class from any element if the clicked element was a .cell. In that case, you need to take care to only register the listener once and not for every instance of .cell.
It is a good practise to always clean-up your listeners upon scope's destruction, in order to prevent memory leaks. You can use scope.$on('$destroy', callback) to remove any event listeners registered on elements.
TABto get to the control, it can't gain focus and there will be no blur event. Usually only input type controls and buttons can get focus.$documentcause that will just create an infinite number of events~mouseleaveto detect when the mouse moves out of the element, but you'd have to register a click handler on the document and trigger the change when you detect a click that is not over that element. What are you trying to do? There may be a better way. Off the top of my head I would think you could maintain a 'selectedCell' property on your scope and the ng-click on your div would just beng-click="selectedCell = cell". Then on your divng-class="{selected : cell == selectedCell}"selectedclass when user click on element itself, or when user set focus on any input in cell?