Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Submit form on pressing Enter with AngularJS

In this particular case, what options do I have to make these inputs call a function when I press Enter?

Html:

<form>     <input type="text" ng-model="name" <!-- Press ENTER and call myFunc --> />     <br />     <input type="text" ng-model="email" <!-- Press ENTER and call myFunc --> /> </form> 
// Controller // .controller('mycontroller', ['$scope',function($scope) {     $scope.name = '';     $scope.email = '';     // Function to be called when pressing ENTER     $scope.myFunc = function() {        alert('Submitted');     }; }]) 
like image 967
ali Avatar asked Mar 14 '13 18:03

ali


People also ask

How do you submit a form when Enter key is pressed?

To submit the form using 'Enter' button, we will use jQuery keypress() method and to check the 'Enter' button is pressed or not, we will use 'Enter' button key code value. Explanation: We use the jQuery event. which to check the keycode on the keypress.

How do you make Enter button on Form submit in angular?

We can use angular keydown event to use Enter key as our submit key. Add keydown directive inside the form tag. Create a function to submit the form as soon as Enter is pressed. Assign the keydown event to the function.

What is Ngform in AngularJS?

The ng-form Directive in AngularJS is used to create a nested form i.e. one form inside the other form. It specifies an inherit control from the HTML form. It creates a control group inside a form directive which can be used to determine the validity of a sub-group of controls.


2 Answers

Angular supports this out of the box. Have you tried ngSubmit on your form element?

<form ng-submit="myFunc()" ng-controller="mycontroller">    <input type="text" ng-model="name" />     <br />     <input type="text" ng-model="email" /> </form> 

EDIT: Per the comment regarding the submit button, see Submitting a form by pressing enter without a submit button which gives the solution of:

<input type="submit" style="position: absolute; left: -9999px; width: 1px; height: 1px;"/> 

If you don't like the hidden submit button solution, you'll need to bind a controller function to the Enter keypress or keyup event. This normally requires a custom directive, but the AngularUI library has a nice keypress solution set up already. See http://angular-ui.github.com/

After adding the angularUI lib, your code would be something like:

<form ui-keypress="{13:'myFunc($event)'}">   ... input fields ... </form> 

or you can bind the enter keypress to each individual field.

Also, see this SO questions for creating a simple keypres directive: How can I detect onKeyUp in AngularJS?

EDIT (2014-08-28): At the time this answer was written, ng-keypress/ng-keyup/ng-keydown did not exist as native directives in AngularJS. In the comments below @darlan-alves has a pretty good solution with:

<input ng-keyup="$event.keyCode == 13 && myFunc()"... />

like image 74
eterps Avatar answered Sep 18 '22 15:09

eterps


If you want to call function without form you can use my ngEnter directive:

Javascript:

angular.module('yourModuleName').directive('ngEnter', function() {         return function(scope, element, attrs) {             element.bind("keydown keypress", function(event) {                 if(event.which === 13) {                     scope.$apply(function(){                         scope.$eval(attrs.ngEnter, {'event': event});                     });                      event.preventDefault();                 }             });         };     }); 

HTML:

<div ng-app="" ng-controller="MainCtrl">     <input type="text" ng-enter="doSomething()">     </div> 

I submit others awesome directives on my twitter and my gist account.

like image 44
EpokK Avatar answered Sep 17 '22 15:09

EpokK