简体   繁体   English

带有按钮的提交,仅在Angular表单外部输入

[英]Submit with button, on enter, outside of form with only Angular

What solution would you implement for an Angular "form", where you wanted it to submit on an enter key press, but you can't rearrange the elements to fit in a <form> element. 您想为Angular“表单”实现什么解决方案,您希望在其中按Enter键提交,但是您不能重新排列元素以适合<form>元素。 For instance something like this: 例如这样的事情:

<div>
    <input/>
    <input/>

    //This might act as a kind of custom built select
    <ul>
        <li></li>
    </ul>

    <form></form> //Dropzone form for instance.

    <button type="submit" ng-click="submitFunction()">Submit</button>
</div>

I want the user to be able to hit enter while the focus is on any of these elements(Not just the true form elements), and have the form submit(validation passing of course). 我希望用户能够在焦点位于任何这些元素(不仅是真正的表单元素)上时按Enter键,并具有表单Submit(当然是通过验证)。 The solution should be purely Angualr(if possible). 解决方案应该纯粹是Angualr(如果可能)。 I'd rather it didn't use complex CSS positioning, I don't want to have to rearrange and muddle the html structure. 我宁愿它不使用复杂的CSS定位,也不想重新排列和弄乱html结构。

I saw a solution online for a custom "ngEnter" type directive that I thought had promise but I don't think everyone on the team is gonna like that solution. 我在网上看到了一个自定义“ ngEnter”类型指令的解决方案,该指令我认为已经实现,但我认为团队中的每个人都不会喜欢该解决方案。 I'd like to know how else one might approach this. 我想知道还有其他方法可以做到这一点。

See this article for more info . 有关更多信息,请参见本文

Here's a snippet showing how to conditionally log to the console using ng-keypress: 这是显示如何使用ng-keypress有条件地登录到控制台的代码段:

 angular.module('myExample', []) .controller('myController', ['$scope', function($scope) { $scope.myFunction = function() { console.log('hi!'); } }]); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="myExample"> <div ng-controller="myController"> <input placeholder="type something" ng-keypress="$event.which === 13 && myFunction()"> </div> </div> 

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

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