在这个特殊的情况下,我有什么选项,使这些输入调用函数时,我按下Enter?

Html:

<form>
    <input type="text" ng-model="name" <!-- Press ENTER and call myFunc --> />
    <br />
    <input type="text" ng-model="email" <!-- Press ENTER and call myFunc --> />
</form>
// Controller //
.controller('mycontroller', ['$scope',function($scope) {
    $scope.name = '';
    $scope.email = '';
    // Function to be called when pressing ENTER
    $scope.myFunc = function() {
       alert('Submitted');
    };
}])

当前回答

另一个方法是使用ng-keypress,

<input type="text" ng-model="data" ng-keypress="($event.charCode==13)? myfunc() : return"> 

用AngularJS - jsfiddle按Enter键提交输入

其他回答

非常好,干净和简单的指令与shift +进入支持:

app.directive('enterSubmit', function () {
    return {
        restrict: 'A',
        link: function (scope, elem, attrs) {
            elem.bind('keydown', function(event) {
                 var code = event.keyCode || event.which;
                 if (code === 13) {
                       if (!event.shiftKey) {
                            event.preventDefault();
                            scope.$apply(attrs.enterSubmit);
                       }
                 }
            });
        }
    }
});

如果你也想要数据验证

<!-- form -->
<form name="loginForm">
...
  <input type="email" ng-keyup="$loginForm.$valid && $event.keyCode == 13 && signIn()" ng-model="email"... />
  <input type="password" ng-keyup="$loginForm.$valid && $event.keyCode == 13 && signIn()" ng-model="password"... />
</form>

这里重要的添加是$loginForm。$valid将在执行函数之前验证表单。您必须为验证添加其他属性,这超出了这个问题的范围。

祝你好运。

我把重点放在表中输入的行下面

<input ng-keydown="$event.keyCode == 13 && onPressEnter($event)" id="input_0" type="text" >

    $scope.onPressEnter = function (event) {
    let inputId = event.target.id;
    let splited = inputId.split('_');
    let newInputId = 'input' + '_' + ((+splited[1]) + 1);
    if (document.getElementById(newInputId))
        document.getElementById(newInputId).focus();
   // else submit form
}

另一个方法是使用ng-keypress,

<input type="text" ng-model="data" ng-keypress="($event.charCode==13)? myfunc() : return"> 

用AngularJS - jsfiddle按Enter键提交输入

FWIW -这是一个我用于基本确认/警报引导模式的指令,不需要<form>

(只需切换出jQuery的点击动作,无论你喜欢什么,并添加data-easy-dismiss到你的模式标签)

app.directive('easyDismiss', function() {
    return {
        restrict: 'A',
        link: function ($scope, $element) {

            var clickSubmit = function (e) {
                if (e.which == 13) {
                    $element.find('[type="submit"]').click();
                }
            };

            $element.on('show.bs.modal', function() {
                $(document).on('keypress', clickSubmit);
            });

            $element.on('hide.bs.modal', function() {
                $(document).off('keypress', clickSubmit);
            });
        }
    };
});