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

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.