get index of active tab

13.3k Views Asked by At

I would like to get the index of the active tab in the JS side.

Here is my code:

HTML:

<uib-tabset>
   <uib-tab ng-repeat="item in tabs" active="item.active" TabIndex="{{item.id}}" heading="{{item.name}}"></uib-tab>
</uib-tabset>

JS:

Category.findAll().$promise.then(function (result) {
      $scope.tabs = result;
    }); 

Here is a screenShot, to show the tabs after lunching the page:

index

I want to get the index: 4 or 3(TabIndex="{{item.id}}") on my js side, onchange but also when the page loads.

2

There are 2 best solutions below

0
On BEST ANSWER

You can bind a scope variable to active property of uib-tabset component:

<uib-tabset active="activeTab">
   <uib-tab ng-repeat="item in tabs" active="item.active" TabIndex="{{item.id}}" heading="{{item.name}}" select="alertMe($event, $index)></uib-tab>
</uib-tabset>

and in your controller you can select the tab or just listening to tab change events:

Category.findAll().$promise.then(function(result) {
    $scope.tabs = result;
    $scope.activeTab = 1; //set 2nd tab

    $scope.$watch('activeTab', function(newVal) {
       console.log(newVal);   //listen to tab change events
    });

 });

See this fiddle if you need.

0
On

select() - An optional expression called when tab is activated. Supports $event in template for expression.

<uib-tabset>
  <uib-tab ng-repeat="item in tabs" active="item.active" TabIndex="{{item.id}}" heading="{{item.name}}" select="alertMe($event, $index)></uib-tab>
</uib-tabset>

alertMe is a function inside controller

$scope.alertMe = function(e, index) {
   console.log(e, index)
 };