简体   繁体   中英

Submit form on pressing Enter with AngularJS using angular material

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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM