25
votes

I have a checkbox and a div on my page.

<input type="checkbox" id="Animals" name="Animals" ng-model="ModelData.Animals" 
                            ng-checked="ModelData.Animals == 'A'" />


 <div class="form-group" ng-show="ModelData.Animals == 'A'">

                        <label for="FirstName" class="col-md-9">
                            Are you interested in Animal Liability Coverage?
                        </label>
                        <div class="col-md-3">
                            <label>
                                <input type="radio" id="AnimalLiabCovY" name="AnimalLiabilityCoverageRadio" ng-model="ModelData.AnimalLiabCov"
                                    value="Y" />
                                Yes
                                <input type="radio" id="AnimalLiabCovN" name="AnimalLiabilityCoverageRadio" ng-model="ModelData.AnimalLiabCov"
                                    value="N" />
                                No
                            </label>
                        </div>
  </div>

I want to show the DIV when the checkbox is selected and hide when deselected. For the first time the above code workd fine ie when checkbox is selected and DIV hides on deselecting the checkbox. But after the first time its not working. It does not show the DIV after selecting the checkbox.

4

4 Answers

26
votes

Have you tried it this way? Here's the fiddle It works great.

<input type="checkbox" id="Animals" name="Animals" ng-model="ModelData.Animals" />
<div class="form-group" ng-show="ModelData.Animals">
  <label for="FirstName" class="col-md-9">
    Are you interested in Animal Liability Coverage?
  </label>
  <div class="col-md-3">
    <label>
      <input type="radio" id="AnimalLiabCovY" name="AnimalLiabilityCoverageRadio" ng-model="ModelData.AnimalLiabCov" value="Y" /> Yes
      <input type="radio" id="AnimalLiabCovN" name="AnimalLiabilityCoverageRadio" ng-model="ModelData.AnimalLiabCov" value="N" /> No
    </label>
  </div>
</div>
11
votes

I think you are looking for ng-true-value and ng-false-value

<div ng-app>
    <div >
        <input type="checkbox" ng-true-value="A" ng-false-value="B" ng-model="check"/>        
    </div>

    <div ng-show="check == 'A'">
        Checked
    </div>
</div>

Fiddle

0
votes
<body ng-app>
     <label>Wolf: <input type="checkbox" ng-model="Wolf" ng-init="checked=true" /></label><br/>
     <label>Tiger: <input type="checkbox" ng-model="Tiger" ng-init="checked=false" /></label><br/>
     <label>Lion: <input type="checkbox" ng-model="Lion" ng-init="checked=false" /></label><br/> 
     Show when checked:
     <div ng-if="Wolf">
     <span> This is removed when the wolf is checked.
     </span>
     </div>

<div ng-if="Tiger">
    <span> This is removed when the tiger is checked.
    </span>
</div>
<div ng-if="Lion">
    <span> This is removed when the lion is  checked.
    </span>
</div>
</body>
0
votes

Input type checkbox return true false in ng-model so i just use this. Its working

   <div ng-app>
   <div >
      <input type="checkbox" ng-model="check"/>        
  </div>

  <div ng-show="check == true">
     Checked
  </div>