bootstrap icon indicating copy to clipboard operation
bootstrap copied to clipboard

Tab selects fired during scope destruction

Open samward1985 opened this issue 12 years ago • 21 comments

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.

samward1985 avatar May 06 '14 16:05 samward1985

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

pkozlowski-opensource avatar May 06 '14 17:05 pkozlowski-opensource

@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 avatar May 06 '14 21:05 samward1985

@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.

samward1985 avatar May 06 '14 21:05 samward1985

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.

onsails avatar May 16 '14 11:05 onsails

Thanks for taking the time to do that, @prettynatty

samward1985 avatar May 16 '14 15:05 samward1985

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.

pursual avatar May 16 '14 18:05 pursual

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

onsails avatar May 17 '14 07:05 onsails

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);
    }; 

chriswu14 avatar May 28 '14 04:05 chriswu14

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

mkeuschn avatar Jun 18 '14 12:06 mkeuschn

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

ghost avatar Jun 26 '14 06:06 ghost

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.

adressin avatar Jul 18 '14 20:07 adressin

Same issue and same version as @adressin .

kdzwinel avatar Jul 25 '14 15:07 kdzwinel

Hello

i fixed by this way (until now work for me) https://github.com/danielgarciagil/bootstrap/commit/2ba12fc484654cf01a82f034c80c4926ed57b3b0

danielgarciagil avatar Jul 28 '14 23:07 danielgarciagil

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

wkjesus avatar Jul 29 '14 21:07 wkjesus

Same here after upgrading to 0.11.

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

PowerKiKi avatar Sep 09 '14 01:09 PowerKiKi

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.

fgarit avatar Sep 26 '14 13:09 fgarit

Same here when upgrading to 0.11 =[

marcosbrigante avatar Sep 26 '14 13:09 marcosbrigante

Encountering this bug :(

Here is a workaround:

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

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

glenn-murray-bse avatar Sep 30 '14 06:09 glenn-murray-bse

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.

mu7amed-abdelfata7 avatar Mar 05 '15 09:03 mu7amed-abdelfata7

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

karianna avatar Mar 05 '15 09:03 karianna