1

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

3
  • You don't. It's all controlled by the is-open binding. Commented Mar 26, 2015 at 23:09
  • @Phil, how can I bind an ID (or something unique) to is-open? I'm looking for something like:is-open="{{group.id}}.open" Commented Mar 26, 2015 at 23:45
  • I'd use a map or something, eg $scope.openGroups = {} and is-open="openGroups[group.id]" Commented Mar 27, 2015 at 0:13

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

@rlsaj another option would have been to create a controller function to search for the group by ID in the array and set its open property to true but I feel like this is simpler

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.