Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular-UI $dialog and form submit on enter key

From what I can see, the recomended way to handle enter key in dialogs in AngularJS is to place a <form> tag and a submit button inside the dialog.

Fair enough, but if you use Angular-UI and their $dialog service, the form will simply close silently when pressing enter. no way to intercept that. even if you attach handlers to ng-click or ng-submit, the form will just close w/o returning any result.

Is there something else I need to do

[Edit]

Solved it, I had to specify explicitly that my "cancel" button was of type "button". Seems like it defaults to "submit" ?

So there was no real problem except for my html form skills :)

like image 578
Roger Johansson Avatar asked Jul 09 '13 14:07

Roger Johansson


1 Answers

To answer my own question. Buttons seems to default to submit(?) and if I explicitly set them to type="button" then they will not trigger postback when pressing enter in an form input field.

<form>
     <input type="text" ... />
     <button type="button" ng-click=...>Cancel</button>
     <button type="submit" ng-click=...>OK</button>
</form>

this way, pressing the enter key in the input field will trigger the ng-click for the OK button.

And as you html hackers have already understood, this had nothing to do with dialogs nor angularjs really, it was a html form issue and my lack of web skills...

like image 177
Roger Johansson Avatar answered Oct 13 '22 07:10

Roger Johansson