Angular UI Bootstrap accordion toggle open all


I'm having an issue with the accordion groups toggle / collapse all.

Expanding collapsing works fine as long as I don't expand /collapse individual accordion groups. I would like the expand, collapse buttons to act as a reset for the accordion groups. That means that regardless of how the individual groups were opened, pressing the collapse / expand all should apply to all groups.

Any suggestions would be most welcomed.


 <button class="btn btn-default btn-sm" ng-click="expandAll()">Expand all</button>
    <button class="btn btn-default btn-sm" ng-click="collapseAll()">Collapse all</button>

    <accordion close-others="false">
            <accordion-group is-open="firstGroupOpen">
                <i class="pull-right glyphicon" ng-class="{'glyphicon-chevron-up': firstGroupOpen, 'glyphicon-chevron-down': !firstGroupOpen}"></i>

    <accordion-group is-open="secondGroupOpen">
                <i class="pull-right glyphicon" ng-class="{'glyphicon-chevron-up': secondGroupOpen, 'glyphicon-chevron-down': !secondGroupOpen}"></i>

Plunker here: Plunk

Problem courtesy of: VictorB


This happens because of how angular works with watching scope variables. There are multiple fixes for this:

  1. Define a scope object and assign all variables to this object (see example below)
  2. Use the controller as syntax (IMO the best solution)
  3. Use $scope.apply() after updating the variables (this triggers the whole digest of angular, so it might be a heavy operation)

Example of first solution:

$scope.accordion = {
  firstGroupOpen: false,
  secondGroupOpen: false

$scope.expandAll = function () {

$scope.collapseAll = function () {

$scope.toggleOpen = function (openAll) {
    $scope.accordion.firstGroupOpen = openAll;
    $scope.accordion.secondGroupOpen = openAll;

See this updated plunker

Solution courtesy of: devqon


There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.