Angular bootstrap ui手风琴组“一次打开一个”不工作

时间:2016-07-04 12:56:35

标签: javascript angularjs angular-ui-bootstrap

我一直在使用Angular ui-bootstrap。在此处oneAtATime即使值设置为true也无法正常工作。这是我的代码。

<div ng-repeat="group in groups track by group.key">
  <uib-accordion close-others="oneAtATime">
    <uib-accordion-group>
      <uib-accordion-heading >
        <div>
          {{ group.title }}
        <i class="pull-right glyphicon" ng-class="{'glyphicon-chevron-down': status.open, 'glyphicon-chevron-right': !status.open}"></i>
        </div>
      </uib-accordion-heading>
    </uib-accordion-group>
  </uib-accordion>
  </div>

plnkr链接。

1 个答案:

答案 0 :(得分:2)

您的HTML结构错误。应该只有一个uib-accordion元素和多个uib-accordion-group元素。所以只需改变你的代码:

<uib-accordion close-others="oneAtATime">
    <uib-accordion-group ng-repeat="group in groups track by group.key">
        <uib-accordion-heading>
            <div>
                {{ group.title }}
                <i class="pull-right glyphicon"
                   ng-class="{'glyphicon-chevron-down': status.open, 'glyphicon-chevron-right': !status.open}"></i>
            </div>
        </uib-accordion-heading>
    </uib-accordion-group>
</uib-accordion>

我所做的只是将您的ng-repeat表达式移到uib-accordion-group元素中。

请参阅下面的工作示例:

&#13;
&#13;
angular.module('ui.bootstrap.demo', ['ngAnimate', 'ui.bootstrap']);
angular.module('ui.bootstrap.demo').controller('AccordionDemoCtrl', function($scope) {
  $scope.oneAtATime = true;

  $scope.groups = [{
    title: 'Dynamic Group Header - 1',
    content: 'Dynamic Group Body - 1',
    key: 1
  }, {
    title: 'Dynamic Group Header - 2',
    content: 'Dynamic Group Body - 2',
    key: 2
  }];

  $scope.items = ['Item 1', 'Item 2', 'Item 3'];

  $scope.addItem = function() {
    var newItemNo = $scope.items.length + 1;
    $scope.items.push('Item ' + newItemNo);
  };

  $scope.status = {
    isCustomHeaderOpen: false,
    isFirstOpen: true,
    open: true,
    isFirstDisabled: false
  };
});
&#13;
<!doctype html>
<html ng-app="ui.bootstrap.demo">

<head>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.js"></script>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular-animate.js"></script>
  <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-1.3.3.js"></script>
  <script src="script.js"></script>
  <link href="//netdna.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
</head>

<body>

  <div ng-controller="AccordionDemoCtrl">
    <div class="checkbox">
      <label>
        <input type="checkbox" ng-model="oneAtATime">Open only one at a time
      </label>
    </div>
    <uib-accordion close-others="oneAtATime">
      <uib-accordion-group ng-repeat="group in groups track by group.key">
        <uib-accordion-heading>
          <div>
            {{ group.title }}
            <i class="pull-right glyphicon" ng-class="{'glyphicon-chevron-down': status.open, 'glyphicon-chevron-right': !status.open}"></i>
          </div>
        </uib-accordion-heading>
      </uib-accordion-group>
    </uib-accordion>
  </div>
</body>

</html>
&#13;
&#13;
&#13;