父状态解析中的函数未触发

Function inside parent state resolve isn't firing

最近几天我一直在与 AngularJS ui-router 作斗争。我试图在每次路线更改时触发功能,但它似乎不起作用。

我有这样的路线:

  .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'
      }
    }
  })

为什么在将路线从 app.home 更改为 app.profile 或相反时,父状态中的函数没有触发?

在 menu.html 中,我直接使用 href="#/app/home

导航到状态

我相信父控制器代码在应用首次加载时 运行 一次。

如果您想在每次更改状态时 运行 执行某些操作(我假设这就是您所说的路由更改的意思),您可以在父控制器中使用类似 $stateChangeSuccess 的事件。

这是一个例子:

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

您可以在此侦听器中调用您想要的函数,然后它应该每次都会触发。