Prevent Tab From Closing


Is there any way to stop a tab from closing? I have a tab and I want to prevent the user from switching to another tab if the input fields on the tab are not valid.

I’ve tried a few experiments using on-deslect and an $on(’$destroy’) in the controller with out success.


hey what about ng-if

How? sorry, I don’t follow what you mean.

check this and see what i mean.


1 Like

Thanks, but this is not what I had in mind.

I have an app with 3 tabs. Here is a pic

We’re on the last tab (person icon). If the email is not valid I want to prevent the user from going to another tab.

The best I’ve been able to do was use a $on.($destroy) that reselected the tab again. But this incurs a controller destroy and recreation that I need to avoid (because user loses all edits)


I think what You need is a good form validation, check out this article

Thanks everyone for the suggestions.

I have form validation, but that alone does not achieve what I need.

I’ll have to use a modal popover to edit the details (I was trying to avoid this as it adds another screen and extra clicks for the user). With a modal I can enable/disable a ‘save’ button based on the form validity, thus forcing the user to complete the form correctly before leaving.

It appears there is no easy way to achieve this at a tab level (ie forcing a user to remain on a tab).


According to the official docs for ui-router (the state provider used for ion-tabs) You can specify a lot of stuff for Your state changes:
UI-Router in depth

If You use $state.go(to [, toParams] [, options]) and then use the options location to set up Your condition:

location Boolean or “replace” (default true), If true will update the url in the location bar, if false will not.

This seems pretty complicated to implement, but I think this will let You avoid that lightbox :smile:

Or You could also catch the event when the change starts and then stop it if the form is not valid:
State Change Events

Thanks for putting me on this this.

I had a play with the state change event. While it might be possible, it would be an ugly hack.

For reference, I tried this to see what would happen in a tab’s controller:

function(event, toState, toParams, fromState, fromParams){

When clicking onto a different tab, the tab changes, but the content of the tab does not load. – meaning it does not prevent the user from leaving a tab. It just stops the destination tab from loading its view.

Hm, that’s interesting how stateChange works :slight_smile: Will You try to use $state.go or a lightbox after all?

Im forced into the lightbox. it’s the simplest way forward.

I like the idea of showing the settings tab icon only when the “test” is passed :slight_smile: