Home Function inside parent state resolve isn't firing
Reply: 1

Function inside parent state resolve isn't firing

Jis0
1#
Jis0 Published in 2018-01-12 14:35:14Z

Last few days I've been struggling with AngularJS ui-router. I am trying to fire function on every route change, but it doesn't seem to work.

I have routes like this:

  .state('app', {
    url: '/app',
    abstract: true,
    templateUrl: 'pages/menu.html',
    resolve: {
      function() { 
        console.log('test');
      }
    }
  })

  .state('app.home', {
    url: '/home',
    views: {
      'menuContent': {
        templateUrl: 'pages/home.html',
        controller: 'homeCtrl'
      }
    }
  })

  .state('app.profile', {
    url: '/profile',
    views: {
      'menuContent': {
        templateUrl: 'pages/profile.html',
        controller: 'profileCtrl'
      }
    }
  })

Why that function inside parent state isn't firing when changing route from app.home to app.profile or vice versa?

Inside menu.html I navigate to state directly with href="#/app/home

DimitrisCSD
2#
DimitrisCSD Reply to 2018-01-12 16:01:30Z

I believe the parent controller code is being ran once when the app is first loaded.

If you want to something to run every time you change a state (I assume that's what you mean by route change), you can use an event like $stateChangeSuccess inside the parent controller.

Here's an example:

$scope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState, fromParams) {
  if (toState.name === 'app.about') {
    console.log('This is the about state!');
  }
}

You could call the function you want inside this listener and then it should fire every time.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.369872 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO