2

I am using the tabs directive of AngularUI Bootstrap with dynamically generated tabs.

<div ng-controller="TabsDemoCtrl">
<uib-tabset active="activeForm">
  <uib-tab  index="$index" ng-repeat="tab in tabs" >
    <uib-tab-heading>{{tab.title}}</uib-tab-heading>
    {{tab.content}}
  </uib-tab>
</uib-tabset>

When the tabs array is changed the UI Tabs get amended accordingly but no active tab is set (although I explicitly do set in in the controller)

angular.module('ui.bootstrap.demo', ['ngAnimate', 'ngSanitize', 'ui.bootstrap']);
angular.module('ui.bootstrap.demo').controller('TabsDemoCtrl', function ($scope, $window) {
  $scope.tabs = [
    { title:'Dynamic Title 1', content:'Dynamic content 1' },
    { title:'Dynamic Title 2', content:'Dynamic content 2' },
    { title:'Dynamic Title 3', content:'Dynamic content 3' },
    { title:'Dynamic Title 4', content:'Dynamic content 4' }
  ];

  $scope.changeTabs = function(){
         $scope.tabs = [
       { title:'Dynamic Title 5', content:'Dynamic content 5' },
       { title:'Dynamic Title 6', content:'Dynamic content 6' }
     ];
     $scope.activeForm = 0; //Not working, how can I select tab dynamically?
  };

  $scope.model = {
    name: 'Tabs'
  };
});

Can anybody tell me what I am missing?

See here for Plunker: https://plnkr.co/edit/Ow7Cd1eidCgaLNOhX1Vl

Thanks in advance Paul

0

2 Answers 2

3

You need to wrap the setting of activeForm in a $timeout:

$timeout(function(){
    $scope.activeForm = 0;
});

This is a known issue - https://github.com/angular-ui/bootstrap/issues/5805

Updated plunkr

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

Comments

1

Hey here is code need to change

$timeout(function(){
 $scope.activeForm = 0; //Not working, how can I select tab dynamically? 
},0);

This will trigger digest cycle and your code will work

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.