简体   繁体   English

ng提交下拉列表和文本区域

[英]ng-submit on dropdown and textarea

Hi I have follow code: 嗨,我有以下代码:

<form ng-submit="ctrl.commitEdit(sign)">
    <input ng-model="sign.Value">
    <input ng-model="sign.Date">
    <textarea ng-model="sign.Comment"></textarea>
    <select ng-model="sign.Property"></select>
    <button type="submit">Save</button>
</form>

With my form around my components I tried to save my edits in two ways (in my form I call in the ng-submit a method from my controller, which saves my edits): 使用围绕组件的表单,我尝试以两种方式保存编辑(在表单中,我从控制器调用ng-submit提交一个方法,以保存所做的编辑):

  • With the click on my button, which has the type "submit", it calls the function in the ng-submit of my form. 单击我的按钮(类型为“提交”)后,它将在表单的ng-submit中调用该函数。 This works correctly! 这正常工作! It saves 节省
  • Then I also would like to save with pressing the "enter" on my keyboard. 然后,我还想通过按键盘上的“输入”来保存。 Thats why I used the form an ng-submit with my button of type submit. 这就是为什么我将ng-submit表单与提交类型的按钮一起使用的原因。 This works only, when I change something in my inputs and the focus is there! 仅当我更改输入中的某些内容且焦点在那里时,此方法才有效! When I change something in my textarea and I press "enter", it makes a break. 当我在文本区域中更改某些内容并按“ enter”(输入)时,它会中断。 When I change something in my dropdown and then press "enter", it opens the dropdown again. 当我在下拉菜单中更改某些内容然后按“输入”时,它将再次打开下拉菜单。

So I would like to save with pressing "enter" in every way, on the input, select and textarea. 因此,我想以各种方式在输入,选择和文本区域上按“输入”来保存。 How can I do this? 我怎样才能做到这一点?

Thanks 谢谢

All you need to do is use ngKeyup. 您需要做的就是使用ngKeyup。

https://docs.angularjs.org/api/ng/directive/ngKeyup https://docs.angularjs.org/api/ng/directive/ngKeyup

Just bind it with enter key code which is 13 and call your function 只需将其与输入密钥代码13绑定,然后调用您的函数

Moreover here is a directive you can implement for your purpose. 此外,这是您可以根据自己的目的实施的指令。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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