Show hidden div on ng-click within ng-repeat

后端 未结 2 1468
情话喂你
情话喂你 2020-12-04 06:36

I\'m working on an Angular.js app that filters through a json file of medical procedures. I\'d like to show the details of each procedure when the name of the procedure is c

相关标签:
2条回答
  • 2020-12-04 07:25

    Use ng-show and toggle the value of a show scope variable in the ng-click handler.

    Here is a working example: http://jsfiddle.net/pvtpenguin/wD7gR/1/

    <ul class="procedures">
        <li ng-repeat="procedure in procedures">
            <h4><a href="#" ng-click="show = !show">{{procedure.definition}}</a></h4>
             <div class="procedure-details" ng-show="show">
                <p>Number of patient discharges: {{procedure.discharges}}</p>
                <p>Average amount covered by Medicare: {{procedure.covered}}</p>
                <p>Average total payments: {{procedure.payments}}</p>
             </div>
        </li>
    </ul>
    
    0 讨论(0)
  • 2020-12-04 07:31

    Remove the display:none, and use ng-show instead:

    <ul class="procedures">
        <li ng-repeat="procedure in procedures | filter:query | orderBy:orderProp">
            <h4><a href="#" ng-click="showDetails = ! showDetails">{{procedure.definition}}</a></h4>
             <div class="procedure-details" ng-show="showDetails">
                <p>Number of patient discharges: {{procedure.discharges}}</p>
                <p>Average amount covered by Medicare: {{procedure.covered}}</p>
                <p>Average total payments: {{procedure.payments}}</p>
             </div>
        </li>
    </ul>
    

    Here's the fiddle: http://jsfiddle.net/asmKj/


    You can also use ng-class to toggle a class:

    <div class="procedure-details" ng-class="{ 'hidden': ! showDetails }">
    

    I like this more, since it allows you to do some nice transitions: http://jsfiddle.net/asmKj/1/

    0 讨论(0)
提交回复
热议问题