0

I am trying to show/hide and adjust the ng-required status if a checkbox is checked, but it doesn't seem to managing the variables properly. Here my examples:

<input type="checkbox" ng-model="checkboxdmodel.value" ng-true-value="'YES'" ng-false-value="'NO'">

Input that I want required unless the checkbox is checked in which case it should be hidden and not required:

<div class="form-group" ng-hide="checked.YES">
                <label class="col-sm-3 control-label" for="inputamount">
                    <font color="red">*</font>Expense Amount</label>
                <div class="col-sm-8">
                    <input type="number" class="form-control" id="inputamount" data-ng-model="itemamount" step="any" ng-required="checked.NO"/>
                </div>
            </div>
4
  • 1
    Is it checkboxdmodel? No typo? And in the ng-hide isn't it checkboxdmodel.checked.YES? Commented Apr 14, 2015 at 17:10
  • Please provide js code also which relates 'v' and 'checked.NO'. Commented Apr 14, 2015 at 17:12
  • 3
    Couldn't you just check for a 'truthy' value on checkboxmodel.value by changing your ng-hide to ng-hide="checkboxmodel.value". If the box is checked this value will be 'true' and the div will be hidden, otherwise it will be shown. Commented Apr 14, 2015 at 17:16
  • I corrected the typo and am using checkbox.value but it is not changing the required behavior for "turning required off" when unchecked. It is working for the ng-show/ng-hide and ng-required that are true when checked. How do I set it to false? Commented Apr 14, 2015 at 18:07

1 Answer 1

1

http://plnkr.co/edit/F4hdqWwhIWiqvsJl5ETx?p=preview

No need for the ng-true and ng-false.

Because checked and unchecked states of the checkbox evaluate to truthy or untruthy, you can just drop the checkbox ng-model value right into wherever you want. In the case of the ng-required, just invert the value with !.

Sign up to request clarification or add additional context in comments.

1 Comment

That did it. Sometimes that answers are more straightforward!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.