AngularJS-如何在不提交表单的情况下使用ng-click? [英] AngularJS - how to use ng-click without submitting the form?
本文介绍了AngularJS-如何在不提交表单的情况下使用ng-click?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个同时具有ng-click和ng-submit的表单.
I have a form that has both ng-click and ng-submit.
ng-submit用于提交,而ng-click则调用单独的功能,例如上载等.
ng-submit is meant for the submission, while ng-click calls a separate function like upload, etc.
如何确保ng-click不会意外提交表单?
How do I make sure that ng-click does not accidentally submits the form?
谢谢!
推荐答案
ngClick
不提交表单.
有时,您可能会遇到问题,因为您在form
中有两个button
元素,并且它们都提交了该元素.为避免这种情况,请在其上指定类型"button"
.示例:
Sometimes, you can have problems because you have two button
elements in your form
, and they both submit it. To avoid that, specify the type "button"
on it. Example :
function demo ($scope) {
$scope.doSubmit = function () {
alert('I submit');
};
$scope.doClick = function () {
alert('I click');
};
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app ng-controller="demo">
<form ng-submit="doSubmit()">
<button type="button" ng-click="doClick()">click</button>
<button type="submit">submit</button>
</form>
</div>
这篇关于AngularJS-如何在不提交表单的情况下使用ng-click?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文