使用角度材料按Enter和AngularJS提交表单

时间:2017-05-01 07:15:37

标签: javascript angularjs angular-material

Submit form on pressing Enter with AngularJS 这个问题使用html5按钮,但我使用Angular Material Button作为

<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>

但如何使用它

1 个答案:

答案 0 :(得分:3)

您可以创建一个Enter指令,如下所示:

'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();
                }
            });
        };
    });

以这种方式使用它:

<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>