Reputation: 803
I use a template that generates a Bootstrap tab layout. Like below:
<div class="a">
<ul class="nav nav-bar nav-stacked" role="tabs">
<li><a href="#home"></a></li>
...
</ul>
<div class="tab-content">
<div id="home">abc</div>
...
</div>
</div>
Now this is pretty simple and straightforward tab navigation that can be hardcoded and achieved.
I have a dynamic ng-repeat
on the ul's
li
and the tab-content's div
s.
The JSON that I get from the REST service is something that contains the data for the tabs and the content to be displayed inside the tab-content within a single object. For eg:
{
"0": "a": [{ // a- tab data
"0": "abc", // abc - data to be displayed inside the tab-content
"1": "xyz"
}]
...
}
With such a JSON hierarchy, I basically need to ng-repeat twice. Once for the ul li
and once for the tab-content as each object of the tab contains the data related to it.
So what I have done so far is:
<div class="a">
<ul class="nav nav-bar nav-stacked" role="tabs">
<li ng-repeat="foo in data"><a href="#home">{{foo.name}}</a></li>
...
</ul>
<div class="tab-content">
<div id="home" ng-repeat="foo in data">
<p ng-repeat="f in foo.desc">{{f}}</p>
</div>
...
</div>
</div>
EDIT:
So my question is, is there a smarter way to achieve this using a single ng-repeat
rather than doing "foo in data" twice?
Sorry if my question isn't clear.
Upvotes: 5
Views: 169
Reputation: 24617
Use ng-include
with $templateCache
rather than ng-repeat
. For example:
var app = angular.module('foo', []);
function foo($templateCache)
{
var model =
{"data":
[
{"name": "Stack",
"desc": ["Exchange", "Overflow"]
}
]
},
cursor, i, bar = baz = "";
for (i = 0; i < model.data.length; i++)
{
bar = bar.concat("<li>", model.data[i].name,"</li>");
baz = baz.concat("<li>", model.data[i].desc.join().replace(/,/g,"</li><li>") );
}
$templateCache.put('name', bar);
$templateCache.put('desc', baz);
}
app.run(foo);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="foo">
<ul ng-include="'name'"></ul>
<ul ng-include="'desc'"></ul>
</div>
References
Upvotes: 1