diff --git a/ionic/components/tabs/tabs.ts b/ionic/components/tabs/tabs.ts index 38408acabd1..65edb2f2bac 100644 --- a/ionic/components/tabs/tabs.ts +++ b/ionic/components/tabs/tabs.ts @@ -287,9 +287,30 @@ export class Tabs extends Ion { * @private */ ngAfterContentInit() { + let preloadTabs = (isBlank(this.preloadTabs) ? this._config.getBoolean('preloadTabs') : isTrueProperty(this.preloadTabs)); + + // get the selected index let selectedIndex = this.selectedIndex ? parseInt(this.selectedIndex, 10) : 0; - let preloadTabs = (isBlank(this.preloadTabs) ? this._config.getBoolean('preloadTabs') : isTrueProperty(this.preloadTabs)); + // ensure the selectedIndex isn't a hidden or disabled tab + // also find the first available index incase we need it later + let availableIndex = -1; + this._tabs.forEach((tab, index) => { + if (tab.enabled && tab.show && availableIndex < 0) { + // we know this tab index is safe to show + availableIndex = index; + } + + if (index === selectedIndex && (!tab.enabled || !tab.show)) { + // the selectedIndex is not safe to show + selectedIndex = -1; + } + }); + if (selectedIndex < 0) { + // the selected index wasn't safe to show + // instead use an available index found to be safe to show + selectedIndex = availableIndex; + } this._tabs.forEach((tab, index) => { if (index === selectedIndex) {