Pawan Kumar Pawan Kumar - 1 month ago 9
AngularJS Question

What is scope of a variable inside ng-repeat block in angular js

what is the scope of showDetails variable. Is it limited to its own li or it affects all the li in the ul.
for complete code refer to http://jsfiddle.net/asmKj/

ul class="procedures" ng-app ng-controller="sample">
<li ng-repeat="procedure in procedures">
<h4><a href="#" ng-click="showDetails = ! showDetails " >{{procedure.definition}} </a></h4>
<div class="procedure-details" ng-show="showDetails">
<p>Number of patient discharges: {{procedure.discharged}}</p>
</div>
</li>



Answer

In the case of ng-repeat, if you create or use a variable like you did with showDetails field , it will create a separate cope for each element type, in this case it will have One for each li .

Now for testing the same , You can create a scope variable with same name as $scope.showDetails and set it default to true, and then run it . You will see that all details will be visible at time of loading , But when you click again ,it will only affect the depending one.

So at first it will a variable for each li element and provide it the value from scope variable.

Check this fiddel Fiddel