[英]Angular submit not called when required fields not filled
I'm creating a form in Angular with Ionic. 我正在用Ionic在Angular中创建一个表单。 I don't want a red error class to be displayed unless a user has submitted the form.
我不希望显示红色错误类,除非用户提交了表单。 As such, my code looks like this:
因此,我的代码如下所示:
<form ng-submit="submit()" name="form">
<ion-radio ng-repeat="item in items" ng-model="data.type" name="type" ng-value="item.value" ng-class="{'error' : form.type.$invalid && formSubmitted }"
</form>
And then in my controllers 然后在我的控制器中
$scope.submit = function ()
{
$scope.formSubmitted = true; //Tell our errors they can show now
}
So the error class should only show up when formSubmitted is true. 因此,仅当formSubmitted为true时,错误类才应显示。 For whatever reason, however,
required
prevents submit
from being called. 但是,无论出于何种原因,都
required
阻止submit
被调用。 This isn't the case for other attributes such as minlength. 其他属性(例如minlength)则不是这种情况。
How can I get the behavior I want? 如何获得我想要的行为?
You need to add novalidate
to your form. 您需要在表单中添加
novalidate
。
The reason being, your browser is validating your form rather than letting your code do it. 原因是,您的浏览器正在验证您的表单,而不是让您的代码来执行它。
alternatively, do something like: 或者,执行以下操作:
<form ng-submit="submit()" name="form" novalidate>
<ion-radio ng-repeat="item in items" ng-model="data.type" name="type" ng-value="item.value" ng-class="{'error' : form.type.$invalid && form.$submitted }"
</form>
form.$submitted
will become true when you submit. form.$submitted
将在您提交时变为true。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.