使用Angular Material展开和折叠列表项

6

有人能给出如何添加展开/折叠视图的建议吗?参见这里。 我想只使用AngularJS的Angular Material,而不依赖于Bootstrap等库。然而在AngularMaterial文档中找不到合适的内容。

谢谢

2个回答

11

一种方法是使用连续的 2 个 md-list-item

以下是 HTML 代码。

  <md-list flex>
   <md-list-item ng-click="toggle.list1 = !toggle.list1">
    <md-icon>explore</md-icon>
    <span flex>Item List 1</span>
    <md-icon ng-show="!toggle.list1">expand_more</md-icon>
    <md-icon ng-show="toggle.list1">expand_less</md-icon>
   </md-list-item>
   <md-list-item ng-repeat="item in data" ng-show="toggle.list1">
   <span flex-offset="5">{{item}}</span>
  </md-list-item>
  <md-list-item ng-click="toggle.list2 = !toggle.list2">
   <md-icon>explore</md-icon>
   <span flex>Item List 2</span>
   <md-icon ng-show="!toggle.list2">expand_more</md-icon>
   <md-icon ng-show="toggle.list2">expand_less</md-icon>
  </md-list-item>
  <md-list-item ng-repeat="item in data" ng-show="toggle.list2">
   <span flex-offset="5">{{item}}</span>
  </md-list-item>
 </md-list>

JS 代码:

angular.module('myApp',['ngMaterial'])
.controller('TempController', function($scope){
 $scope.data = [ "Item 1", "Item 2", "Item 3", "Item 4"]
 $scope.toggle = {};
});;

这里是可工作的Codepen


1
为了使展开/折叠过渡动画更加生动,您可以考虑使用ngAnimate。只需将section-show-hide类添加到隐藏元素中,并添加以下CSS代码即可:.section-show-hide { transition: all linear 0.5s; max-height: 500px; } .section-show-hide.ng-hide { opacity: 0; max-height: 0; } - Wiil

2

你知道是否有一个只使用AngularMaterial的示例吗?我现在无法添加额外的库。 - Adam
是的,没错。这不是用材料完成的。这个链接是否更有帮助?http://blog.sodhanalibrary.com/2016/02/accordion-with-angularjs-material-ui.html#.WKxqI1UrJaQ - Dominik Heim

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接