简体   繁体   English

在角度js中动态添加角度删除所需的验证

[英]add remove required validation in angular dynamically in angular js

I have a form as shown below, I have kept only 1 field here for simplicity. 我有一个如下所示的表单,为简单起见,我在这里只保留了1个字段。 In my form I have many other fields including a country dropdown. 在我的表格中,我有许多其他领域,包括国家下拉列表。 If country selected is A,B or CI want to remove the required validation which I do using a addressRequired which gets a true/false value on change of country. 如果选择的国家/地区是A,B或CI想要删除我使用addressRequired进行的所需验证,该addressRequired变更会在国家/地区更改时获得真/假值。

I am removing the required using .removeAttr() and it does get remove from the html. 我正在删除所需的.removeAttr() ,它确实从html中删除。

But the ng-disabled="myAbcForm.$invalid" stills shows invalid. ng-disabled="myAbcForm.$invalid"剧照显示无效。

<form name="myAbcForm" ng-submit="submitForm(myAbcForm.$valid)" novalidate class="myAbcForm">
   <div class="element-box fltlt">
        <label>Address </label><sup ng-show="addressRequired">*</sup>
        <input type="text" value="" ng-model="details.Address" required="">
        <span ng-show="!details.Address && addressRequired" class="error-msg field-validation-error">Address is required.</span>
    </div>
    <div id="clearfix">
        <input type="submit" value="Update" class="btn fltlt btnBG" ng-disabled="myAbcForm.$invalid" />
    </div>
</form>

Any suggestions ? 有什么建议 ?

Use ngRequired instead, like: 请改用ngRequired ,例如:

ng-required="checkCountry()"

http://jsfiddle.net/coma/0jz49knd/1/ http://jsfiddle.net/coma/0jz49knd/1/

With a little hint from coma's answer 昏迷的 答案中得到一点暗示

Here is what I am using now 这是我现在使用的

<input type="text" value="" 
  ng-model="details.Address" 
  ng-required="addressRequired" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM