AngularJS $ state.go()不会发出$ stateChangeSuccess?

时间:2015-11-09 12:48:43

标签: javascript angularjs angular-ui-router state

我有控制器B$state.go('state_b');驻留在不同的状态。

当我调用state_b时,Angular会将状态更改为B并加载控制器$stateChangeSuccess。一切如预期。不期望的是我的B控制器中没有B

为什么我的$stateChangeSuccess未从控制器$state.go获得A的{​​{1}}?

B控制器是否还有其他方式可以知道它的显示? (以前哪个状态处于活动状态并不重要,只是状态/控制器B再次处于活动状态)

州定义:

.state("state_b", {
  url: "b/?:id",
  templateUrl:"/template/cms/shared/tabs/genericView.html.twig",
  reloadOnSearch: false,
  moduleName: "Module B",
  componentName: "Component B",
  parent: "application"
})
.state("state_a", {
  url: "a/",
  templateUrl: "/template/bundlename/a/index.html.twig",
  moduleName: "Module A",
  componentName: "Component A",
  parent: "application",
  controller: "OohAController as a",
  resolve: {
    b: function(OohBService, OohActiveBService) {
      var bId = OohActiveBService.getActiveB();
      return OohAService.get({id: bId}).$promise;
    }
  }
})

1 个答案:

答案 0 :(得分:2)

我会说,这个概念正在发挥作用。有a working example

此事件将被解雇

.run(['$rootScope', '$state', function($rootScope, $state) {
    $rootScope.$on('$stateChangeSuccess', function(evt, to, params, from) {
      console.log("from : " + from.name + ", to:" + to.name);
    });
}])

任何类似的过渡

<a ui-sref="state_b({id:1})">
<a ui-sref="state_b({id:22})">
<a ui-sref="state_a">

我做的唯一重要更改是网址

  .state("state_b", {
      // url: "b/?:id",
      url: "/b/?:id",
      templateUrl: 'tpl.html',
  })
  .state("state_a", {
      //url: "a/",
      url: "/a/",
      templateUrl: 'tpl.html',
      controller: 'OohAController as a',
  })

检查here