Kode
Kode

Reputation: 3215

AngularJS check if checkbox is unchecked

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>

Upvotes: 0

Views: 4191

Answers (1)

tpie
tpie

Reputation: 6221

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

Upvotes: 1

Related Questions