Angular JS ng-click验证 [英] Angular JS ng-click validation
本文介绍了Angular JS ng-click验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望单击提交按钮后,如果ng-class不将span颜色更改为red,则检查是否以输入形式写下了一些内容.我只想在提交后一直保持红色,这是链接:
I want to have that after clicking submit button it check if there is something written down in input form if not the ng-class change the span color to red . I want to have red color only after submittion not all the time , here is link :
<form name="myForm" validate class="form-horizontal">
<div class="control-group" >
<label ng-class="{bad: ( myForm.name.$invalid && myForm.name.$pristine ) }" >Name:</label>
<input type="text" name="name" placeholder="Name" ng-model="user.name" required/>
<span ng-show="isInvalid('name')" class="help-inline">Name is required</span>
<span ng-show="isValid('name')">Great!</span>
</div>
<button type="submit">SUB</button>
</form>
http://plnkr.co/edit/m3dqBnpPenbY65xa3PXH?p=preview
推荐答案
您需要跟踪是否单击了提交按钮,在这里您可以维护范围变量并检测是否提交了表单
you need to track whether submit button clicked or not , here u can maintain a scope variable and detect the form is submitted or not
<button type="submit" ng-click="submitted = true">SUB</button>
如果有人点击了提交,则存在一个名为submitted
且具有true
值的作用域变量
if some one clicked the submit then, there is a scope variable called submitted
with true
value
<label ng-class="{bad: ( myForm.name.$invalid && myForm.name.$pristine && submitted ) }" >Name:</label>
这是柱塞
这篇关于Angular JS ng-click验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文