我有这个 plunkr,它有一个动态添加标签的系统。
当我添加一个新选项卡时,它被设置为活动选项卡。当我添加两个选项卡,单击第一个选项卡并添加一个新选项卡时,问题就出现了。使用此顺序,新插入的选项卡不会设置为活动状态。
这是html代码:
<div ng-controller="TabsDemoCtrl">
{{activeTabIndex}}
<uib-tabset active="activeTabIndex">
<uib-tab ng-repeat="tab in tabs" index="tab.id">
<uib-tab-heading>{{ tab.title }} <a ng-click="removeTab(tab.id)" href=''>(del)</a> </uib-tab-heading>
{{tab.content}}
</uib-tab>
</uib-tabset>
<div ng-controller="BtnCtrl">
<div class="btn btn-primary" ng-click="addTab()">Add</div>
</div>
</div>
和控制器:
angular.module('ui.bootstrap.demo', ['ngAnimate', 'ngSanitize', 'ui.bootstrap']);
angular.module('ui.bootstrap.demo').controller('TabsDemoCtrl', function ($rootScope, $scope) {
$rootScope.activeTabIndex = 0;
$rootScope.tabs = [];
//Close tab
$scope.removeTab = function(index)
{
var tabsLength = $scope.tabs.length -1;
for (var i=0; i < $scope.tabs.length; i++)
{
if ($scope.tabs[i].id == index)
{
$scope.activeTabIndex = $scope.tabs[tabsLength].id;
$scope.tabs.splice(i, 1);
}
}
};
})
angular.module('ui.bootstrap.demo').controller('BtnCtrl', function ($timeout, $scope, $rootScope) {
//Add new tabs
$scope.addTab = function()
{
var newTab = {
title: 'new tab',
content: 'content here',
id: makeid()
};
$rootScope.tabs.push(newTab);
$timeout(function()
{
$rootScope.activeTabIndex = newTab.id;
});
}
function makeid()
{
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < 5; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
});