26

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.

0

4 Answers 4

26

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Tyler. I knew what I was missing.
11

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

Comments

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

Comments

0

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>

Comments

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.