如何在ng-sumit中使用ui-keypress?

时间:2017-01-21 12:29:17

标签: angularjs forms ng-submit

我有一个这样的表格

<form name="job" ng-submit="createJob(job,job_)" novalidate>

<!-- some other code -->

<!-- add new requirement to job_.requirement array on-enter --> 
<input type="text" class="form-control" ng-model="requirement" ui-keypress="{13:'addRequirement(job_)'}">

<!-- display requirements  -->

<ul>
    <li ng-repeat="requirement in job_.requirement track by $index" >{{requirement.name}}</li>
</ul>

<!-- submit entire form  -->
<button type="submit">Create Job</button>

</form>

因为我在输入新要求时使用ui-keypress(在key-press =&gt; 13上添加新要求),表单才会提交。我有什么方法摆脱它?

创建了一个演示fiddle

1 个答案:

答案 0 :(得分:0)

您可以使用ng-keyup在点击输入按钮

时提交表单
<form name="job" ng-submit="createJob(job,job_)" ng-keyup="$event.keyCode == 13  && createJob(job,job_);" novalidate>

<!-- some other code -->

<!-- add new requirement to job_.requirement array on-enter --> 
<input type="text" class="form-control" ng-model="requirement" ui-keypress="{13:'addRequirement(job_)'}">

<!-- display requirements  -->

<ul>
    <li ng-repeat="requirement in job_.requirement track by $index" >{{requirement.name}}</li>
</ul>

<!-- submit entire form  -->
<button type="submit">Create Job</button>

</form>