1

When do controllers get instantiated? Is it the first time you visit that state? also, What happens when you revisit the state, does a new controller get instantiated again?

Assume that I have two states, A and B, and I put an alert statement at the top of state B. I noticed that if go from state A to B state B's alert statement sets off which tells me that the controller got instantiated. But suppose I go from state A to B to C and back to B, the alert statement does NOT go off. However, if I go from state A to B to C to B to A to B the alert statement goes off again.

Here is a part of my routes:

state A = app.login

state B = app.pincodeCreate

state C = app.messagelist

.run ($ionicPlatform, startup) ->
  $ionicPlatform.ready(startup.ionicReady)

.config (googleAnalyticsCordovaProvider, $stateProvider, $urlRouterProvider) ->

  $stateProvider

    .state('app', {
      url: '/app',
      abstract: true,
      templateUrl: 'templates/menu.html',
      controller: 'AppController'
    })

    .state('app.pincodeCreate', {
      url: '/pincode',
      views: {
        menuContent: {
          templateUrl: 'templates/pincode.html',
          controller: 'PincodeController'
        }
      }
    })

    .state('app.login', {
      url: '/login',
      views: {
        menuContent: {
          templateUrl: 'templates/login.html',
          controller: 'LoginController'
        }
     }
   })
   .state('app.messagelist', {
      url: '/messagelist',
        views: {
          menuContent: {
            templateUrl: 'templates/messagelist.html',
            controller: 'MessageListController',
            resolve: {
              activities: (utils, store, $state) ->
                utils.getActivities().then ((activities) ->
                  store.isUserLoggedIn(true)
                 activities
              ), (error) ->
              $state.reload()
           }
        }
      }
    })
3
  • please give something that shows what you're trying to say, or at least some code. What do you mean by "state"? Commented Mar 18, 2015 at 1:15
  • Did my answer address your question? Commented Mar 23, 2015 at 3:18
  • New Dev, Yes your answer addressed my question. Thank you. Commented Mar 23, 2015 at 17:58

1 Answer 1

5

The view's controller for a particular state runs when you go from state outside of the hierarchy tree of that state to that state or one of its descendants.

In other words, if, say, you have the following hierarchy:

     A     B
    /     /
   AA    C
        / \
       C1 C2

Then, switching from A to B would instantiate B. Switching then to C (or C1 or C2, for that matter), and then back to B, would not re-instantiate B's controller.

If you switch to A (or AA), then A would instantiate. Then switching back to B would re-instantiate B.

So, most likely in your case, C is a child state of B. And A and B are in separate ancestry trees.

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

1 Comment

Thank you very much for this explanation. It helped understanding our problem :)

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.