我在带有required属性的输入文本框中有以下代码,但是当我选择该字段或提交表单时,它不会阻止表单提交并通知用户该字段是必需的。< / p>
<div class="col-sm-8">
<input type="text" ng-required="true" class="form-control"
placeholder="Enter Total Amount" id="txtTotalAmount"
ng-model="formCtrl.AddCheckDeposit.TotalAmount" />
</div>
我需要做些什么才能使必需的指令起作用?
答案 0 :(得分:3)
为此,您应该在表单有效时触发ng-submit
事件
ng-submit="myForm.$valid && submit()"
似乎您还错过了输入字段中的name
属性,也显示了您可以使用ng-show
/ ng-messages
指令的错误
<form name="myForm" ng-submit="myForm.$valid && submit()">
<div class="col-sm-8">
<input type="text" ng-required="true" class="form-control" placeholder="Enter Total Amount" name="txtTotalAmount"
id="txtTotalAmount" ng-model="formCtrl.AddCheckDeposit.TotalAmount" />
<span ng-show="myForm.txtTotalAmount.$error.required">Required</span>
</div>
</form>