将条件运算符与 ng-class - AngularJS 一起使用

Using conditional operator with ng-class - AngularJS

我是网络开发的新手。最近正在尝试在 Angular JS 中实现 Tab 功能的示例。

想对以下代码应用条件运算符我可以应用条件运算符吗? 因为我做对了。它显示了一些错误。

  <div class="tab-content">
    <div ng-class="{'tab-pane active': activeTab === 1, 'tab-pane' : activeTab !== 1}">Panel 1 Content</div>
    <div ng-class="{'tab-pane active': activeTab === 2, 'tab-pane' : activeTab !== 2}">Panel 2 Content</div>
    <div ng-class="{'tab-pane active': activeTab === 3, 'tab-pane' : activeTab !== 3}">Panel 3 Content</div>
  </div>

请从下面找到完整的代码:

我正在尝试让 Tab 动态点击。这样一来,单击选项卡后,相应的窗格就会处于活动状态。

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Bootstrap tab panel with Angular</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" /> 
  </head>
  <body ng-app="app" ng-controller="ctrl">
      <ul class="nav nav-tabs">
        <li ng-class="{'active' : activeTab == 1}"><a href="" ng-click="setActiveTab(1)">One</a></li>
        <li ng-class="{'active' : activeTab == 2}"><a href="" ng-click="setActiveTab(2)">Two</a></li>
        <li ng-class="{'active' : activeTab == 3}"><a href="" ng-click="setActiveTab(3)">Three</a></li>
      </ul>

      <div class="tab-content">
        <div ng-class="{'tab-pane active': activeTab === 1, 'tab-pane' : activeTab !== 1}">Panel 1 Content</div>
        <div ng-class="{'tab-pane active': activeTab === 2, 'tab-pane' : activeTab !== 2}">Panel 2 Content</div>
        <div ng-class="{'tab-pane active': activeTab === 3, 'tab-pane' : activeTab !== 3}">Panel 3 Content</div>
      </div>
      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
      <script>
        angular.module("app", [])
            .controller("ctrl", ['$scope', function($scope) {
                $scope.activeTab = 1;

                $scope.setActiveTab = function(tabToSet) {
                    $scope.activeTab = tabToSet;
                }
            }]);
      </script>
  </body>
</html>

您可以将其最小化为 ng-class 条件内的单个表达式,方法是添加 tab-pane 作为默认值 class 并在选项卡为 active 时有条件地添加 class活跃。

 class="tab-pane" ng-class="{'active': activeTab === 1}