angularjs - Angular,如何按输入键运行函数?

标签 angularjs function

我试图找出一种在 Angular 中通过从表单输入中按 Enter 来运行函数的好方法。我在stackoverflow帖子上发现了这个:

    $(".input1").keyup(function (e) {
        if (e.keyCode == 13) {
            // Do something
        }
    });

但我想知道是否有更好的方法来做到这一点。

最佳答案

我使用这样的指令:

.directive('ngEnter', function () {
    return function (scope, element, attrs) {
        element.bind("keydown keypress", function (event) {
            if (event.which === 13) {
                scope.$apply(function () {
                    scope.$eval(attrs.ngEnter);
                });
                event.preventDefault();
            }
        });
    };
});

然后,在 HTML 中,您可以执行以下操作:
<input type="text" ng-model"whatever" ng-enter"submitAFunction()">        

关于angularjs - Angular,如何按输入键运行函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38250451/

相关文章:

javascript - Angularjs 资源 URL

angularjs - Angular-Translate 部分加载器不工作

php - contenteditable 在表 : select cells

javascript - 如何在函数内返回 node.js 中导入的模块?

java - 给定乘数的下一个数字倍数

python - 在这种情况下如何使用 Assert?

javascript - Protractor 切换到上一个选项卡

javascript - angular - $http 第一次调用 super 慢

javascript - 将 "resolve"放在 Angular-ui-router View 上不起作用 - 页面根本无法渲染

javascript - 如何创建和调用接受三个参数(页面长度)并计算 block 面积的 JavaScript 函数