Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

Commit 7d3ba1e

Browse files
Casey Garlandwesleycho
Casey Garland
authored andcommitted
fix(tabs): ensure tab selection only occurs once
- Fix issue where select callback is fired twice due to compilation order when using dynamic tabs mixed with static tabs Closes #3060 Closes #4230 Fixes #2883
1 parent 790d6b9 commit 7d3ba1e

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

src/tabs/tabs.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,15 @@ angular.module('ui.bootstrap.tabs', [])
1818
if (tab.active && tab !== selectedTab) {
1919
tab.active = false;
2020
tab.onDeselect();
21+
selectedTab.selectCalled = false;
2122
}
2223
});
2324
selectedTab.active = true;
24-
selectedTab.onSelect();
25+
// only call select if it has not already been called
26+
if (!selectedTab.selectCalled) {
27+
selectedTab.onSelect();
28+
selectedTab.selectCalled = true;
29+
}
2530
};
2631

2732
ctrl.addTab = function addTab(tab) {

src/tabs/test/tabs.spec.js

+38-7
Original file line numberDiff line numberDiff line change
@@ -191,15 +191,12 @@ describe('tabs', function() {
191191
});
192192

193193
describe('ng-repeat', function() {
194-
beforeEach(inject(function($compile, $rootScope) {
194+
var $compile, $rootScope;
195+
beforeEach(inject(function(_$compile_, _$rootScope_) {
196+
$compile = _$compile_;
197+
$rootScope = _$rootScope_;
195198
scope = $rootScope.$new();
196199

197-
function makeTab(active) {
198-
return {
199-
active: !!active,
200-
select: jasmine.createSpy()
201-
};
202-
}
203200
scope.tabs = [
204201
makeTab(), makeTab(), makeTab(true), makeTab()
205202
];
@@ -214,6 +211,13 @@ describe('tabs', function() {
214211
scope.$apply();
215212
}));
216213

214+
function makeTab(active) {
215+
return {
216+
active: !!active,
217+
select: jasmine.createSpy()
218+
};
219+
}
220+
217221
function titles() {
218222
return elm.find('ul.nav-tabs li');
219223
}
@@ -263,6 +267,33 @@ describe('tabs', function() {
263267
scope.$apply();
264268
expectTabActive(scope.tabs[2]);
265269
});
270+
271+
it('should not select twice', function() {
272+
elm.remove();
273+
elm = null;
274+
scope = $rootScope.$new();
275+
276+
scope.tabs = [
277+
makeTab(), makeTab(), makeTab(true), makeTab()
278+
];
279+
scope.foo = {active: true};
280+
scope.select = jasmine.createSpy();
281+
elm = $compile([
282+
'<tabset>',
283+
' <tab ng-repeat="t in tabs" active="t.active" select="select()">',
284+
' <tab-heading><b>heading</b> {{index}}</tab-heading>',
285+
' content {{$index}}',
286+
' </tab>',
287+
' <tab active="foo.active" select="select()">',
288+
' <tab-heading><b>heading</b> foo</tab-heading>',
289+
' content foo',
290+
' </tab>',
291+
'</tabset>'
292+
].join('\n'))(scope);
293+
scope.$apply();
294+
295+
expect(scope.select.calls.count()).toBe(1);
296+
});
266297
});
267298

268299
describe('advanced tab-heading element', function() {

0 commit comments

Comments
 (0)