d3bug3r
d3bug3r

Reputation: 2586

AngularJS checkbox uncheck

How in angular when a checkbox is uncheck, the form data will be reset.

      <div class="form-group">
                <input type="text" verify-store ng-model="user.merchant_store_name" class="form-control" name="merchant.store_name" placeholder="Store Name" ng-model-options="{ updateOn: 'blur' }" required>
                <div ng-if="signupForm.$pending.usernameExists">verifying....</div>
                <div ng-if="signupForm.$error.usernameExists[0].$viewValue">
                <label class="control-label" style="color:red">A store with that name already exists. Please select another name.</label>
            </div>

        <div class="form-group">
                <input type="text" class="form-control" ng-model="user.merchant_mobile_phone" placeholder="Phone" >
        </div>

            </div>


<div class="checkbox clip-check check-primary">
    <input type="checkbox" id="agree"  ng-model="signupForm.agree" value="agree" ng-required="!signupForm.agree">
    <label for="agree">I agree</label>
</div>

 <div class="checkbox clip-check check-primary" ng-show="signupForm.agree">
        <input type="checkbox" id="merchant"  ng-model="signupForm.merchant" value="merchant" >
        <label for="merchant">Sign up as merchant</label>
  </div>

Is there a way this can be done within the view template rather than passing it to controller. Or should I write a directive for this?

Thanks!!

Upvotes: 0

Views: 283

Answers (1)

Charlie
Charlie

Reputation: 23778

You don't have to write a single line in your controller to achieve your task with the following two DOM settings.

<input type="text" ng-model="myText">
<input type='checkbox' ng-model='chkMonitor'  ng-click='!chkMonitor && (myText = "")' >

Explanation:

Your inputs are bound to the scope through scope variables. Now you place your code in ng-click of the check box to execute the clearing code. This has to be done only when the check box is unchecked. So, first check for this in a boolean expression before executing your clearing code :

ng-click='!chkMonitor && (myText = "")'

Your myText = "" will never execute as long as the chkMonitor is false.

Upvotes: 2

Related Questions