bootstrap icon indicating copy to clipboard operation
bootstrap copied to clipboard

$modal takes command outside of popup

Open atul221282 opened this issue 11 years ago • 4 comments

When popup is open and nothing is focused and selected pressing backspace triggers the route change event. Currently i am using the directive to disable any navigation but I was hoping for something built in with modal object?

my directive code is var app = angular.module("ControlDirectives", []); app.directive('disableNavigation', ["$document", function ($document) { return { restrict: 'AE', scope: {//create isolate scope so that locationChangeStart get destroyed for this modal //otherwise we will end up same event getting fired twice on two different location }, link: function (scope, element, attr) { scope.currentUrl = $document[0].URL; scope.$on("$locationChangeStart", locationChange); function locationChange(event, next, current) { if (next !== scope.currentUrl) { event.preventDefault(); } } } }; }]);

And within pop html i got form tag decorated with that directive my popup html

atul221282 avatar Sep 15 '15 05:09 atul221282

Theres nothing built in to $modal service as browser's back button or backspace cannot events can not be prevented. you have two options here, one is to use stateful modals, you can find more about this approach going to the link below if you are using ui router for the routing.

https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions#how-to-open-a-dialogmodal-at-a-certain-state

Second approach is to automatically dismiss all modals on state transition. you can accomplish this by adding $stateChangeStart event in your app run function.

App.run([
  '$rootScope', '$modalStack', 
  function($rootScope, $modalStack) {
    $rootScope.$on('$stateChangeStart', function() {
      var top = $modalStack.getTop();
      if (top) {
        $modalStack.dismiss(top.key);
      }
    });
  }
]);

ehteshamkafeel avatar Sep 15 '15 09:09 ehteshamkafeel

Thanks for you reply and it is really appreciated but that still doesn't solve my problem as I have mentioned that I don't want to close the modal and I am also not using ui router.

atul221282 avatar Sep 15 '15 10:09 atul221282

The modal is a standalone component with no knowledge of routing - this is a feature that is ideally in a custom component or service for your application to keep concerns separate.

wesleycho avatar Sep 16 '15 13:09 wesleycho

I agree

atul221282 avatar Sep 17 '15 00:09 atul221282