0

I am trying to use bootstrap tab with angularjs & have added ng-click to set active class on tab but its not happening, below is my code:

<ul class="nav nav-tabs" role="tablist">
        <li role="presentation" ng-class="{ 'active': tab === 1 }"><a href="#email" aria-controls="email" role="tab" data-toggle="tab" ng-click="tab = 1">Email</a></li>
        <li role="presentation" ng-class="{ 'active': tab === 2 }"><a href="#phone" aria-controls="phone" role="tab" data-toggle="tab" ng-click="tab = 2">Phone</a></li>
        <li role="presentation" ng-class="{ 'active': tab === 3 }"><a href="#username" aria-controls="username" role="tab" data-toggle="tab" ng-click="tab = 3">Username</a></li>
    </ul>

tab's value is not changing and that's why the particular class is not getting applied.

2 Answers 2

1

Just change === to ==, it should works:

<ul class="nav nav-tabs" role="tablist">
    <li role="presentation" ng-class="{ 'active': tab == 1 }"><a href="#email" aria-controls="email" role="tab" data-toggle="tab" ng-click="tab = 1">Email</a></li>
    <li role="presentation" ng-class="{ 'active': tab == 2 }"><a href="#phone" aria-controls="phone" role="tab" data-toggle="tab" ng-click="tab = 2">Phone</a></li>
    <li role="presentation" ng-class="{ 'active': tab == 3 }"><a href="#username" aria-controls="username" role="tab" data-toggle="tab" ng-click="tab = 3">Username</a></li>
</ul>

JSFiddle here.

Sign up to request clarification or add additional context in comments.

2 Comments

Actually I have the router as well, so that #email, #phone is creating issue, its changing the route, so how I can disable it?
I think you should post another question for this problem (and we will need your JS file where you define your routes). :)
0

In your controller, try to use

$scope.obj = { tab : 1};

and then in the view:

obj.tab

instead of just tab

2 Comments

still no luck, its same
When page reloads this check ng-class="{ 'active': tab === 1 }" works fine, but then onclick tab's value is not changing

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.