I am basically wanting to make a right click menu which will record comments on the clicked element. How do I make a contextmenu which closes when I click anywhere else except when I click on the contextmenu elements. With the code I have, even on click the elements which are of the contextmenu, it closes.
<div prevent-right-click visible="isVisible">
asd
</div>
<div ng-if="isVisible">
<div class="btn-group-vertical" role="group" aria-label="Vertical button group">
<button type="button" class="btn btn-default">Button</button>
<button type="button" class="btn btn-default">Button</button>
<div class="btn-group" role="group">
<button id="btnGroupVerticalDrop1" type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> Dropdown <span class="caret"></span> </button>
<div class="dropdown-menu pull-left newdropdown" aria-labelledby="btnGroupVerticalDrop1">
<form>
<input type="text" placeholder="Enter name"> <br>
<input type="text" placeholder="Enter comment">
</form>
</div>
</div>
</div>
</div>
Here is my AngularJs code
app.controller('mainCtrl', function($scope, $http, $timeout, $interval, $log){
$scope.isVisible = false;
$scope.$watch('isVisible', function(){
$log.log($scope.isVisible)
})
});
app.directive('preventRightClick', function() {
return {
restrict: 'A',
scope: {
visible: '='
},
link: function($scope, $ele) {
$ele.bind("contextmenu", function(e) {
e.preventDefault();
$scope.$apply(function () {
$scope.visible = true;
})
});
$(document).on('click', '*', function (event) {
$scope.$apply(function () {
$scope.visible = false;
})
})
}
};
})
My JsFidde link: https://jsfiddle.net/ywf7kL5y/