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

Tab selects fired during scope destruction #2155

Closed
samward1985 opened this issue May 6, 2014 · 21 comments
Closed

Tab selects fired during scope destruction #2155

samward1985 opened this issue May 6, 2014 · 21 comments

Comments

@samward1985
Copy link

After updating to 0.11, I'm having issues with the tab select function.

I have a function as follows:

<tab select="logOut()">

I'm finding that logOut() fires whenever I click any link in any of the tab content areas.

@pkozlowski-opensource
Copy link
Member

@sjdw80 please provide a minimal reproduce scenario using http://plnkr.co/, otherwise we can't help.

@samward1985
Copy link
Author

@pkozlowski-opensource

I'll work on that. In the meantime, I've narrowed down the behavior a bit. It looks as though, if I use the $location service within any ng-click functions in the tabset to navigate away from the page, the select function for each tab is fired.

@samward1985
Copy link
Author

@pkozlowski-opensource

It doesn't look at though plinkr will let me use the $location service normally, so I can't really demonstrate it there. The problem is with the following line:

ctrl.select(tabs[newActiveIndex]);

Found in TabsetController. It looks as though, when the $location service is used to navigate away, the removeTab function is being called on each tab:

scope.$on('$destroy', function() {
     tabsetCtrl.removeTab(scope);
});
        ctrl.removeTab = function removeTab(tab) {
            var index = tabs.indexOf(tab);
            //Select a new tab if the tab to be removed is selected
            if (tab.active && tabs.length > 1) {
                //If this is the last tab, select the previous tab. else, the next tab.
                var newActiveIndex = index == tabs.length - 1 ? index - 1 : index + 1;
                ctrl.select(tabs[newActiveIndex]);
            }
            tabs.splice(index, 1);
        };

Which will lead to each tab being sequentially "selected" and each tab select function being fired as the user navigates away from the page.

@chrisirhc chrisirhc changed the title Tab Selects Tab selects fired during scope destruction May 7, 2014
@onsails
Copy link

onsails commented May 16, 2014

I am having the same issue. Here is plunk: http://plnkr.co/edit/O8TKIS?p=preview
Press "Disable tabs" and see alert from the select callback.

@samward1985
Copy link
Author

Thanks for taking the time to do that, @prettynatty

@pursual
Copy link

pursual commented May 16, 2014

Just discovered this issue as well. Since select is fired on every tab on every tab it is hard to keep track of the last selected when redrawing.

@onsails
Copy link

onsails commented May 17, 2014

I am changing url on tab select and the app is becoming insane when I destroy scope containing tabs.

@chriswu14
Copy link

In my case I have two tabs, first tab is default to selected and I have server call wired to each tab's selection. if I leave the page by clicking back button the second tab selected call will be made to the server. This is because when the tab is being destroyed the remaining one wants to be selected automatically. BUT if I change tab then leave the page nothing will happen because the following code only triggers if the active one is removed first(I guess).

Just some discoveries

 ctrl.removeTab = function removeTab(tab) {
        var index = tabs.indexOf(tab);
        //Select a new tab if the tab to be removed is selected
        if (tab.active && tabs.length > 1) {
            //If this is the last tab, select the previous tab. else, the next tab.
            var newActiveIndex = index == tabs.length - 1 ? index - 1 : index + 1;
            ctrl.select(tabs[newActiveIndex]);
        }
        tabs.splice(index, 1);
    }; 

lancecaraccioli added a commit to lancecaraccioli/right-angles that referenced this issue Jun 4, 2014
- added showcase service
- added workaround for tab.select misbehaviour
-- issue: angular-ui/bootstrap#2155
-- workaround: use ng-click to signify selection of tab
@mkeuschn
Copy link

I have the same problem when I am navigating away from the site where the tabs are located the events are fired.
I have created a question on SO (http://stackoverflow.com/questions/24279287/tab-select-event-is-triggered-when-i-am-navigating-away).

And here the plunk
http://plnkr.co/edit/7NQwVGa7tpBDNRbIckZk?p=preview

@ghost
Copy link

ghost commented Jun 26, 2014

I have this issue too I believe. It prevents the user from using the back button on their browser when the back button destination is a state (using ui-router) on a different parent. It appears to go to the correct location but then immediately goes back to the tabs page (without parameters) and fires off all the tab states

@adressin
Copy link

I'm also experiencing this with 0.11.0. I save the current tab to local storage when selected, so this issue is causing the incorrect tab to be stored.

@kdzwinel
Copy link

Same issue and same version as @adressin .

@danielgarciagil
Copy link

Hello

i fixed by this way (until now work for me)
danielgarciagil@2ba12fc

@wkjesus
Copy link

wkjesus commented Jul 29, 2014

i have the same problem but with a modal....when modal close or dismiss, then trigger the tabselected, here is the plunkr: http://plnkr.co/edit/TXBAHvAAnB8xaWoD2dfB?p=preview

@PowerKiKi
Copy link
Contributor

Same here after upgrading to 0.11.

@danielgarciagil could you submit a pull request with your fix, please ?

@fgarit
Copy link

fgarit commented Sep 26, 2014

I ran into this issue today as well.
When navigating away, when the $scope is destroyed, the select of each tab is called one after the other.

@marcosbrigante
Copy link

Same here when upgrading to 0.11 =[

@glenn-murray-bse
Copy link

Encountering this bug :(

Here is a workaround:

var destroying = false;
$scope.$on('$destroy', function() {
    destroying = true;
});

$scope.tabSelectMethod = function() {
    if(!destroying) {
        // doStuff()
    }
}

ulle pushed a commit to ulle/bootstrap that referenced this issue Oct 22, 2014
OronNadiv pushed a commit to lanetix/bootstrap that referenced this issue Nov 18, 2014
@mu7amed-abdelfata7
Copy link

The problem is still remaining and did not fix but i made a solution for me to solve the problem:
1- I added this function in TabsetController:

var destroyed = false;
ctrl.destroyTab = function destroyTab(tab) {
console.info("ctrl.destroyTab = function destroyTab(tab) {");
destroyed = true;
ctrl.removeTab(tab);
};

2-I calling destroyTab() rather than removeTab() in $on('$destroy') in tab directive:
scope.$on('$destroy', function() {
tabsetCtrl.destroyTab(scope);
});

please see if this solution is proper for the directive or not.

@karianna
Copy link
Contributor

karianna commented Mar 5, 2015

@mu7amed-abdelfata7 Can you open a new issue with a plunkr please.

@wesleycho
Copy link
Contributor

I am not seeing this issue currently here.

Feel free to open another issue with a reproduction if possible as suggested.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests