简体   繁体   中英

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. 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.

I am removing the required using .removeAttr() and it does get remove from the html.

But the ng-disabled="myAbcForm.$invalid" stills shows 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:

ng-required="checkCountry()"

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" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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