0

I have the following Modal dialog using ui.boostratp & anuglar modal service

<div id="modalDialog" class="modal-dialog">
    <div class="modal-header">
        <h2 style="text-align: center">{{modalOptions.headerText}}</h2>
    </div>
    <div class="modal-body">
        <p>{{modalOptions.bodyText}}</p>
    </div>
    <div class="modal-footer">
        <button type="button" class="btn" data-ng-click="modalOptions.close()">{{modalOptions.closeButtonText}}</button>
        <button type="button" id="OK" class="btn btn-danger" ng-enter="modalOptions.ok();" autofocus data-ng-click="modalOptions.ok();" data-ng-keyup="$event.keycode == 13 && modaloptions.ok()">{{modalOptions.actionButtonText}}</button>
    </div>
</div>
<script type="text/javascript">
       $(document).ready(function () {
           console.log('Modal Template Loaded');
           $('#OK').focus();

           $("#modalDialog").keydown(function (event) {
               console.log("Event mapped")
               if (event.keyCode == 13) {
                   $(this).parent()
                          .find("button:eq(0)").trigger("click");
                   return false;
               }
           });

       }); //document

</script>

I tried out multiple ways, but none of it worked. after the dialog box loads the 'Modal Template Loaded' is logged to console. the form works using Mouse though, but want it to work for enter key. how do i get it working for Enter Key ?

user3799325
  • 590
  • 1
  • 8
  • 20

2 Answers2

2

I figured out that the Key event were being captured on the Parent form and not in the dialog box.

So i wrote a JavaScript to trigger click from parent window like shown below.

document.onkeypress = function (e) {
                console.log("key Press " + e.keyCode);

                if (e.keyCode == 13)
                    $("#ModalOKButton").trigger("click");
            };
user3799325
  • 590
  • 1
  • 8
  • 20
  • This works fine, thanks!. It seems angular modal service doesn't handle the focus very well. – Guille May 18 '17 at 15:44
0

You can wrap the modal in form tags and then use ng-submit. Note that you should use ng-submit on its own, without ng-click.

Also see this: Angular-UI $dialog and form submit on enter key

Community
  • 1
  • 1
chinaowl
  • 532
  • 5
  • 15