2

Submit form on pressing Enter with AngularJS This question uses html5 buttons but I'm using Angular Material Button as

<div class="card-action no-border text-right">
  <md-button class="color-primary sign-btn" ng-disabled="logForm.$invalid" ng-click="login()"> Sign In</md-button>
</div>

But how to work with it

Community
  • 1
  • 1

1 Answers1

3

You can create an Enter directive as below:

'use strict';

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

                    event.preventDefault();
                }
            });
        };
    });

And use it in this way:

<div class="card-action no-border text-right">
  <md-button class="color-primary sign-btn" eopd-enter="login()" ng-disabled="logForm.$invalid" ng-click="login()"> Sign In</md-button>
</div>
Prateek Gupta
  • 1,129
  • 1
  • 11
  • 24