我有一个可折叠div,它是用angularjs创建的,在这里,我可以在script.js内的json数组的帮助下打开可折叠div的第一部分,但是我想用其他方式打开它,而不是用json。下面是代码
https://plnkr.co/edit/nCdGzZYPSTYsMPYf8K9o?p=preview
HTML格式

  <script src='https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.1/angular.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/angular-filter/0.5.4/angular-filter.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.1/angular-sanitize.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/2.5.0/ui-bootstrap-tpls.js'></script>
<script  src="script.js"></script>
<link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap.css'>
 <body ng-app="app">
    <h1>Dynamic accordion: nested lists with html markup</h1>
  <div ng-controller="AccordionDemoCtrl">
    <div>
      <div  ng-repeat="group in groups track by $index">
        <div class="parents"  ng-click="open($index)"><i ng-class="{'glyphicon-minus': group.isOpen, 'glyphicon-plus': !group.isOpen}"></i> {{ group.title }}
        </div>
        {{ group.content }}
        <ul class="childs" ng-show="group.isOpen">
          <li ng-repeat="item in group.list">
            <span ng-bind-html="item"></span>
          </li>
        </ul>
      </div>
    </div>
  </div>
  </body>

脚本
angular.module('app', ['ui.bootstrap','ngSanitize','angular.filter']);
angular.module('app').controller('AccordionDemoCtrl', function ($scope) {
  $scope.oneAtATime = true;

  $scope.open = function (index) {
    $scope.groups[index].isOpen = !$scope.groups[index].isOpen;
    $scope.closeOthers(index);
  }

  $scope.closeOthers = function (index) {
    for(var i = 0; i  < $scope.groups.length; i++) {
       if (i !== index)
        $scope.groups[i].isOpen = false;
    }
  }

  $scope.groups = [
    {
      title: 'title 1',

      isOpen: true,
      list: ['<i>item1a</i> blah blah',
        'item2a',
        'item3a']
    },
    {
      title: 'title 2',

      list: ['item1b',
        '<b>item2b </b> blah ',
        'item3b']
    },
    {
      title: 'title 3',

    },
    {
      title: 'title 4',

    },
    {
      title: 'title 5',

    }
  ];

});

最佳答案

您可以使用angular.forEach并尝试:
JS公司:

 $scope.groups[0].expanded = true;
   $scope.expanded = function(select) {
             var boolexpansion = !select.expanded;
             angular.forEach($scope.groups, function(n) {
             n.expanded = false;
            });
            if(boolexpansion) {
            select.expanded = !select.expanded;
        }
    }

HTML格式:
<div ng-repeat-start="group in groups track by $index">
      <button ng-if="!group.expanded" ng-click="expanded(group);">+</button>
      <button ng-if="group.expanded" ng-click="expanded(group);">-</button>
      {{ group.title }}
</div>
   <div ng-if="group.expanded" ng-repeat-end="" >
   {{group.content}}
   </div>
</div>

示例:https://next.plnkr.co/edit/rCx31w3q5i52brTx

关于html - 如何在angularjs中保持可折叠div的第一部分,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54044551/

10-13 00:25