Nested ng-repeat in Angularjs displayed at the same level -
i have json i'll using generate html displaying tabs , buttons.
a simplified version of json this:
[{"id": "a","buttons":[{"id":"a1"},{"id":"a2"}]}, {"id": "b","buttons":[{"id":"b1"},{"id":"b2"}]}]
which store in $scope.navigation is.
and, ideally i'd generate this:
<span id="tabs"> <div>a</div> <div>b</div> </span> <span id="buttons"> <div>a1</div> <div>a2</div> <div>b1</div> <div>b2</div> </span>
what tried this
<span id="tabs"> <div ng-repeat="tab in navigation">{{tab.id}}</div> </span> <span id="buttons"> <span ng-repeat="tab in navigation"> <div ng-repeat="button in tab.buttons">{{button.id}}</div> </span> </span>
of course not work creates span element separates buttons depending tab belong to. there way need?
thanks!
what's purpose of it? < div> inside < span> considered bad practice , i'd advise change css if appropriate. there may reasoning though, question doesn't give enough details answer.
and coming above problem can achieved creating array consists of buttons below.
$scope.navigation= [{ "id": "a", "buttons": [{ "id": "a1" }, { "id": "a2" }] }, { "id": "b", "buttons": [{ "id": "b1" }, { "id": "b2" }] }]; $scope.buttons = []; angular.foreach($scope.navigation, function (tab) { angular.foreach(tab.buttons, function (button) { $scope.buttons.push(button); }); });
and use ng-repeat below.
<div> <div data-ng-repeat="button in buttons">{{button.id}}</div> </div>
Comments
Post a Comment