0

I have a screen-1 ('home' screen) which has three tabs (say tab1, tab2, and tab3). When app launches, the 'tab3' is active (in homeController.js I have set "$scope.active='tab3'").

Now when I open 'tab1' (so 'active' class updated to 'tab1') and redirects from this 'tab1' to another screen-2 ($location.path('/pathToScreen2') using routing), the screen-2 launches which has a back button, onclick of back button ($location.path('/pathToScreen1')), it navigates back to screen-1.

Now when screen-1 is launched, the active tab is 'tab3' rather I want 'tab1' should be active (probably every time when homeController gets loaded, It would initialise "$scope.active='tab3'"). How can I accomplish this?

2 Answers 2

1

With the help of Mikki, below is how I achieved this.

ActiveTabService:

angular.module("DemoApp")
.service('ActiveTabService', function(){
        var activeTab = 'tab3';
        return {
            getActiveTab: function(){
                return activeTab;
            },
            setActiveTab: function(tab){
                activeTab = tab;
            }
        }
});

Here is the complete demo with routing and multiple screens.

Sign up to request clarification or add additional context in comments.

Comments

0

You should not save states inside controllers variables, because controller has it's own lifecycle of destroying with all it's defined variables.

So what you need here - is create some kind of state service and initialize there your active tab. Inject this service across all controllers related to tab functionality, and set to this state service active tab once user entered to some controller.

P.S. Services are singletons, so if you inject service to few controllers they will all have links to the same object and it will not be destroyed before you will do it manually

1 Comment

Thanks for the idea Mikki :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.