9

I don't seem to be able to set the inital tab in an angular bootstrap tabset. It always sets the left most tab to active.

Given the html:

<tabset>
    <tab heading="Static 1" active="data.static1">Static content</tab>
    <tab heading="Static 2" active="data.static2">Static content</tab>
</tabset>

and js:

angular.module('plunker', ['ui.bootstrap']);
var TabsDemoCtrl = function ($scope) {
  $scope.data = {static1: false, static2: true}
};

See the Plunker

Update 6 Aug 2013: Now fixed upstream see the github issue.

1
  • I added a new answer because it is still an issue in version 0.6.0 and this question is high in google. Commented Nov 14, 2013 at 16:04

4 Answers 4

13

It seems like (static) tabs overwrite whatever is passed to active when the directive is run. I assume it's a bug. Quick and dirty, you can use a timeout with 0 seconds delay to set the active state. At least in the plunkr, this does not cause any flicker. In your controller:

$scope.data = {};
$timeout(function() {
  $scope.data.static2 = true;  
}, 0)

http://plnkr.co/edit/3KbdKh?p=preview

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

Comments

3

There is an issue in all versions of angular-ui / bootstrap upto Version 0.6.0 :

http://plnkr.co/edit/73lm068buZf851h47FVQ?p=preview

it works in the bootstrap3 branch which you have to build yourself:

http://plnkr.co/edit/uOASvZ71DzgZqODmHQP8?p=preview

1 Comment

thanks for the info. In case people are curious pull#834 fixed this issue. The deletion of lines 237-239 are the key changes (I patched my version of angular ui with those changes - I independently came to the same fix conclusion).
3

I had this issue today and I found the shortest way to get around it was to set it using ng-init:

<tabset justified="true" ng-init="tabs[initialTab].isActive = true">
    <tab heading="Static 1" active="tabs.Inprogress.isActive"></tab>
    ...

1 Comment

Arghhh - thank you, thank you. I could select any tab other than the first programmatically - with ng-init, even that first one now works. I've lost 2 hours trying - so frustrating!
1

Your code works in the latest versions of ui-bootstrap (tested versions 0.7.0 through 0.13.0 inclusive):
http://plnkr.co/edit/SzeTqXVSd8CiKL9nkjDC?p=preview

HTML:

<!DOCTYPE html>
<html ng-app="myApp">

  <head>
    <script data-require="[email protected]" data-semver="1.4.0" src="https://code.angularjs.org/1.4.0/angular.js"></script>
    <script data-require="[email protected]" data-semver="0.13.0" src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.13.0.min.js"></script>
    <link data-require="bootstrap-css@*" data-semver="3.3.1" rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" />
    <script src="script.js"></script>
  </head>

  <body ng-controller="ExampleController as example">
    <tabset>
      <tab heading="Static 1" active="data.static1">Static content1</tab>
      <tab heading="Static 2" active="data.static2">Static content2</tab>
    </tabset>
  </body>

</html>

JavaScript:

angular.module("myApp", ["ui.bootstrap"])
  .controller("ExampleController", function ($scope) {
    $scope.data = {
      static1: false,
      static2: true
    };
  });

One gotcha to be aware of is that this will not work if the ngController directive is placed on the <tabset> element. In the code above, I have placed the ngController directive on the <body> element.

Comments

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.