Angularjs Ng-repeat 2d Array And Display Only Certain Values Based On Index
I have the the following two dimensional array: SideNavItems= [['Parent Section 1', 'Parent Section 2'],['Parent Section 3', 'Parent Section 4']] I want to use ng-repeat to displ
Solution 1:
In my opinion, you really want to keep this out of the UI and hide it away in the controller. You already have an action called toggleSelect in your controller by the looks of things, inside that you can set a new scope property to be only the side nav items of the currently selected tab, something like;
$scope.toggleSelect = function(index) {
// existing code here
$scope.currentTabSideNavItems = SideNavItems[index];
}
And your markup can be simplified to:
<divid="field"><divng-repeat="item in currentTabSideNavItems"><ang-click="level=2">{{item}}</a></div></div>
That way the logic for extracting out the items to be displayed is hidden nicely in your controller, and can be tested.
Solution 2:
As @DoctorMick said, it's better to "separate logic from view" I made a snippet on jsfiddle with your needs, hope this helps.
<sectionng-app="App"ng-controller="Ctrl"><nav><ul><ling-repeat="tab in tabs"ng-click="updateSideNavItems($index)"ng-class="{active: $index === currentIndex}"><a>{{tab}}</a></li></ul></nav><divid="field"><divng-repeat="cell in SideNavItems">
{{cell}}
</div></div></section>
var app = angular.module('App', []);
app.controller('Ctrl', function ($scope) {
varSideNavItems = [
["Parent Section 1", "Parent Section 2"],
["Parent Section 3", "Parent Section 4"]
];
$scope.tabs = ['tab1', 'tab2', 'tab3'];
$scope.updateSideNavItems = functionupdateSideNavItems(index) {
$scope.currentIndex = index;
if (index === 0) $scope.SideNavItems = SideNavItems[0];
elseif (index === 1) $scope.SideNavItems = SideNavItems[1];
else $scope.SideNavItems = [].concat.apply([], SideNavItems);
}
// defaults
$scope.currentIndex = 0;
$scope.updateSideNavItems($scope.currentIndex);
});
Post a Comment for "Angularjs Ng-repeat 2d Array And Display Only Certain Values Based On Index"