2

I am new to web development. Recently was trying out an example to implement Tab functionality in Angular JS.

I want to apply conditional operator to the following code. Can I apply condtional operator? cause I dint get it right. Its showing some error.

  <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>

Please find the whole code from below:

I am trying to get Tab click Dynamically. So that upon tab clicks corresponding panes are active.

<!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>
2
  • What is the error you are getting? The code seems to work without any javascript errors in JSFiddle. Commented Apr 10, 2016 at 13:40
  • I was trying something like ng-class="{ activeTab === 1 ? 'tab-pane active' : 'tab-pane'}" . Which i believe is wrong. Commented Apr 10, 2016 at 13:43

1 Answer 1

4

You could minimize it to single expression inside ng-class condition, by adding tab-pane as default class and add active class conditionally when tab is active.

 class="tab-pane" ng-class="{'active': activeTab === 1}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the concise line of code. Working perfectly. I had previously did some thing like below using conditional operator. ng-class="{ activeTab === 1 ? 'tab-pane active' : 'tab-pane'}" . Which i believe is wrong.
That seems correct to me...but unnecessarily you are adding tab-pane class...which would be there always..rather do add active class conditionally
got your point. Will follow as you suggest. Thanks for your advise.
@NareshRaj though the code which you were trying should be ng-class=" activeTab === 1 ? 'tab-pane active' : 'tab-pane'".. Glad to help you. Thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.