【问题标题】:Angular - toggle show/hide content not workingAngular - 切换显示/隐藏内容不起作用
【发布时间】:2015-08-06 11:07:57
【问题描述】:

我在 Angular 中创建了一个显示/隐藏内容的小功能,因此当单击菜单项/选项卡时,它会显示所需的内容,效果很好。

现在,我的问题是我需要最初(在页面加载时)显示所有选项卡的全部内容,以便所有选项卡/菜单都处于活动状态。

此外,当单击菜单时,如果没有任何选项卡/菜单处于活动状态,它应该再次显示全部内容并使所有选项卡处于活动状态。

$scope.toggleGroup = function (group) {
        if ($scope.isGroupShown(group)) {
           $scope.shownGroup = null;
       } else {
           $scope.shownGroup = group;
        }
  };

  $scope.isGroupShown = function (group) {
    return $scope.shownGroup === group;
  };

工作代码:PLUNKR

有人可以帮忙吗?

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:

    试试这个: $scope.isGroupShown = 功能(组){ 返回 $scope.shownGroup === 组 || $scope.shownGroup == null; };

    所以 isGroupShown() 将为每个组返回 true,而 shownGroup 为空

    更新: 没有注意到您在项目单击句柄中使用 isGroupShown 函数。 所以你需要另一个函数,或者只是在 ng-show 指令中更复杂的表达式,像这样:

    <div ng-repeat="content in availability" ng-show="isGroupShown(content.name) || shownGroup == null">
        {{content.name}} : {{content.issue}}
    </div>
    

    PLNKR link

    【讨论】:

    • 您的解决方案不起作用。尽管它显示了全部内容,但我无法单击单个菜单:(
    【解决方案2】:

    将控制器更新为下面的示例

    var app = angular.module('plunker', []);
    
    app.controller('MainCtrl', function($scope) {
    
      $scope.showAllGroups = true;
    
      $scope.legend = [
          {"name":"critical"},
          {"name":"info"},
          {"name":"ok"},
          {"name":"unknown"},
          {"name":"warning"}
      ];
    
    
      $scope.availability = [
          {"name":"critical", "issue":"1111"},
          {"name":"info","issue":"2513"},
          {"name":"ok","issue":"3569"},
          {"name":"unknown","issue":"1245"},
          {"name":"warning","issue":"4598"},
          {"name":"critical", "issue":"7899"},
          {"name":"info","issue":"3265"},
          {"name":"ok","issue":"7415"},
          {"name":"unknown","issue":"0213"},
          {"name":"warning","issue":"3333"}
      ];
    
      $scope.toggleGroup = function (group) {
            $scope.showAllGroups = false;
            if ($scope.isGroupShown(group)) {
               $scope.showAllGroups = true;
               $scope.shownGroup = null;
           } else {
               $scope.shownGroup = group;
            }
      };
    
      $scope.isGroupShown = function (group) {
        if ($scope.showAllGroups)
          return true;
        return $scope.shownGroup === group;
      };
    });
    

    【讨论】:

    • 不错...但是如果用户在 div 内单击它会再次显示整个内容,这不是很直观我的想法是仅在加载时显示整个内容,并且只有当您单击两次时在同一个菜单/选项卡上。
    • 查看更新的答案。您不再需要在中继器上使用 onClick。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多