问题
I have an accordion:
<accordion>
<accordion-group ng-repeat="group in groups" heading="{{group.title}}" id="{{group.id}}" is-open="group.open">
<table class="table">
<tr>
<td>{{group.definition}}</td>
</tr>
</table>
</accordion-group>
</accordion>
and each accordion has a unique ID.
I know how to open an accordion based on its position:
<button ng-click="groups[0].open = !groups[0].open">Toggle Title 1 based on index</button>
but how do I open an accordion based on its ID?
My current attempt is here: http://plnkr.co/edit/c3GeaWfOgZ2YoQb2kUbW
回答1:
Following on from my comment, try something like this...
In your controller, create a map to store open group information
$scope.accordionGroups = {};
If you want one group to default open, try this
$scope.accordionGroups[groups[0].id] = true;
Then use this in your template
<accordion-group ng-repeat="group in groups"
heading="{{group.title}}" id="{{group.id}}"
is-open="accordionGroups[group.id]">
To toggle a group with your button, use this
<button ng-click="accordionGroups.title1 = !accordionGroups.title1">Toggle title1</button>
Plunker
来源:https://stackoverflow.com/questions/29290429/open-angular-ui-bootstrap-accordion-based-on-id