ps0604 ps0604 - 4 months ago 44
AngularJS Question

How to select a tab on Angular UI programmatically?

In this plunk I have three tabs that are created from an array. When I click on the button, I need the one named "Name 1" to be selected and have the focus. How can this be achieved?

HTML

<uib-tabset>
<uib-tab ng-repeat="t in tabs" heading="{{t.title}}" >
{{t.content}}
</uib-tab>
</uib-tabset>


<br/><br/><br/>
<button ng-click="tabs.select(1)">Focus on Name 1</button>


Javascript

$scope.tabs = [
{ title:'Name 0', content: "Content 0" },
{ title:'Name 1', content: "Content 1" },
{ title:'Name 2', content: "Content 2" }
];

Answer

You have to setting the active attribute, as below:

<uib-tabset active="active">

Then in your button:

<button ng-click="active = 1">Focus on Name 1</button>

Here's the forked Plunker.