0

The user of my web should enter a simple code using buttons in my form and after that he should click Ok or hit enter and continue with proccess.

The thing is that the enter key is not submiting my form, is executing other method that is called from ng-click. How can avoid enter key call the ng-click method?

myapp.js

var myApp = angular.module('myApp',[]);

myApp.controller('myformcontroller', ['$scope', function ($scope){
    // Procesing data from form.
    $scope.signin = function () {
    }
}]);

myApp.controller('mycontroller', ['$scope', function ($scope){
   $scope.do = function() {
       alert('ng-click pressed!');
   }
}]);

myform.html

<div ng-controller="myformcontroller">

    <form name="myForm"
          role="form" 
          ng-submit="signin()" 
          novalidate>

        <input type="text"/>

        <div ng-controller="mycontroller">
            <a href="javascript:void(0);" ng-click="do()">
                clickme!
            </a>
        </div>

        <button type="submit">Ok</button>
    </form>

</div>

Press click me and hit enter key after on My Fiddle

C1pher
  • 1,933
  • 6
  • 33
  • 52
napstercake
  • 1,815
  • 6
  • 32
  • 57

2 Answers2

0

It is not blocking. You're missing a standard input to bubble the properly handled click event. If there is no input, the form is not submitted by default. It's not an angular issue. Take a look here https://stackoverflow.com/a/477699/4573999

EDIT
More detailed explanation based on comments. The question is not about the submit element. Sure, you have a button type submit. The for other elements inside the form there is a default behavior. And for input element the default on enter key is to submit the form that it is surrounded by. But for the anchor tag it is not. Because it is not supposed to provide any data. Why would you want to submit a form on a link? So in your case you should submit the form programmatically by calling appropriate method.

myApp.controller('myformcontroller', ['$scope', function ($scope){
    $scope.signin = function () {
               alert('submit!');
    }
}]);

myApp.controller('mycontroller', ['$scope', function ($scope){
   $scope.do = function() {
       alert('ng-click pressed!');
       $scope.signin();
   }
}]);

Check updated fiddle
If you'd like to get this in a generic way, you can parse the ng-submit value like here How to programmatically submit a form with AngularJS

Community
  • 1
  • 1
Kirill Slatin
  • 6,085
  • 3
  • 18
  • 38
0

You can ommit html rules with jquery. Add id to a form and button first:

<form id="mydiv" name="myForm"
    role="form" 
    ng-submit="signin()" 
    novalidate>

    <div ng-controller="mycontroller">
       <a href="javascript:void(0);" ng-click="do()">
          <img src="img/a-letter.png" alt="">
        </a>
    </div>

    <button id="mydiv2" type="submit">Ok</button>
</form>

And then set listener on enter up:

$("#mydiv").keyup(function(event){
if(event.keyCode == 13){
    $("#mydiv2").click();
}
});

So it's not elegant, but you can submit form with simulating click on enter pressed.

changtung
  • 1,614
  • 15
  • 19