By creating a personalized directive, you can now hide the right sidebar by clicking outside of the top menu.
For a demonstration, visit: http://jsfiddle.net/zqdmny41/20/
app.controller('mainCtrl', function ($scope) {
// Function to toggle off the right sidebar.
$scope.hideSideMenu = function() {
$scope.bodyCon = false;
$scope.noneStyle = false;
}
...
})
.directive("outsideClick", ['$document', function( $document){
return {
link: function( $scope, $element, $attributes ){
var scopeExpression = $attributes.outsideClick,
onDocumentClick = function(event){
if(event.target.id != 'rightMenu' && event.target.id != 'toggle-menu') {
$scope.$apply(scopeExpression);
}
};
$document.on("click", onDocumentClick);
$element.on('$destroy', function() {
$document.off("click", onDocumentClick);
});
}
}
}]);
In your HTML:
<aside class="rightbar" id="rightMenu" ng-class="{'noneStyle' : noneStyle}" outside-click="hideSideMenu()">
This implementation allows for:
- Toggling the right sidebar by clicking the top icon,
- No action when clicking on the right sidebar itself,
- Hiding the right sidebar by clicking elsewhere.
The key concept involves:
- Developing a custom directive named "outsideClick" and attaching it to the right sidebar menu with the attribute
outside-click="hideSideMenu()"
- Passing the function name
hideSideMenu
to the directive, which binds a click event to the document.
- If a user clicks anywhere on the page and the target ID is neither
rightMenu
nor toggle-menu
, the right sidebar will be concealed.
Source: