I'm using the following directive:
app.directive("ngOutside", function($document, $rootScope){
return {
restrict: "A",
link: function(scope, elem, attr, ctrl){
elem.bind("click", function(e){
e.stopPropagation();
});
$document.bind("click", function(){
$rootScope.$apply(function(){
$rootScope.nav = false;
$rootScope.aside.on = false;
});
});
}
}
});
Now I apply this in my HTML like so:
<nav ng-outside>
<ul>
<li><a href="/home">Home</a></li>
<li><a href="/dashboard">Dashboard</a></li>
</ul>
</nav>
Originally I was using hashbang mode and the directive worked fine. Now I am using HTML5 mode and the e.stopPropagation
is stopping Angular working out the routing when one of the nav links is clicked. Instead, the browser now treats the link normally and does a full page reload to the location.
If I remove the following, it solves the problem but the directive no longer does it's job:
elem.bind("click", function(e){
e.stopPropagation();
});
How can I implement the 'click anywhere but here' type directive without breaking links in HTML5 mode?