使用AngularJS中的按钮进行条件必需的验证 [英] Conditional Required Validation with buttons in AngularJS

查看:42
本文介绍了使用AngularJS中的按钮进行条件必需的验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个按钮,分别是保存"和提交",还有一些下拉菜单和文本框.需要单击提交"而不是单击保存"来验证某些字段.请在下面找到代码.

I having two buttons as Save and Submit and some of the drop-downs and text-boxes. There is need to validate some of the fields on click of Submit, not on click of Save. Please find the code in below.

<button class="btn btn-default custom_edit" 
                            data-ng-click="orderForm.$valid && saveOrder('save')" 
                            data-ng-if="!order.IsSubmitted"
                            data-ng-model="status"
                            value="save">Save</button>
<button class="btn btn-success" 
                            data-ng-click="orderForm.$valid && saveOrder('submit')" 
                            data-ng-if="!order.IsSubmitted"
                            data-ng-model="status"
                            value="submit">Submit</button>

 <input type="text" class="form-control" name="_requisition" placeholder="Requisition"
                                        data-ng-model="order.Requisition"
                                        data-ng-trim="true"
                                        data-ng-required="status=='save'"/>

我尝试使用值&带有按钮的模型,并使用ng-required进行应用,但是它不起作用.

I have tried by using value & model with buttons and applied it with ng-required , however it's not working.

推荐答案

使用输入类型作为提交",可以保持验证,也可以在表单有效时调用其他功能.

Using input type as 'submit', validation can be maintained as well as other function can be also called as form is valid.

<input type="submit" value="Save"
                            data-ng-click="isSave = true; orderForm.$valid && saveOrder('save')" 
                            data-ng-model="isSave" 
                            data-ng-show="!order.IsSubmitted"/>
<input type="submit" value="Submit"
                            data-ng-click="isSave = false; orderForm.$valid && saveOrder('submit')" 
                            data-ng-model="isSave"  
                            data-ng-show="!order.IsSubmitted"/>

<input type="text" class="form-control" name="_requisition" placeholder="Requisition"
                            data-ng-model="order.Requisition"
                            data-ng-trim="true"
                            maxlength="100"
                            data-ng-required="isSave"/>

因此,通过这种方式,我们可以在运行时通过单击按钮轻松设置模型值,并可以验证表单.

So by this way, we can easily set model value on button click at run time as well as can validate form.

这篇关于使用AngularJS中的按钮进行条件必需的验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆