Search code examples
angularjsangularjs-ng-repeatrootscope

$rootScope variable in ng-repeat Angularjs


I have two different table templates. I keep the visibility of the table using ng-show='template == 1' directive in first table and ng-show='template == 2'inside the second table. I set the template to either 1 or 2 on the controller. I build the rows as ng-repeat = "det in $root.tradedetails" in both the tables and assign the json to the tradedetails from the controller. Can I use the same rootscope variable $root.tradedetails for the both the table template. For example, if I want to iterate the first table, I will set the template = 1 and assign the details to $root.tradedetails. It will automatically set the display of second table to none. I want to know whether the ng-repeat inside the second table iterate over the $root.tradedetails.


Solution

  • The simple answer is yes - you can have multiple ng-repeats iterating over the same collection.

    All ng-show does is add a css class of ng-hide to the elements where the result of the ng-show expression is false. This hides the element, but it is still present in the DOM.

    So with the set up you have described, you would have two tables in the DOM, both with content from $rootscope.tradedetails, and they would be shown/hidden based on the value of $scope.template.