przemo_li
przemo_li

Reputation: 4053

AngularJS $state.go() do not emit $stateChangeSuccess?

I have controller A and B residing in separate states.

When I invoke $state.go('state_b'); Angular will change state to state_b and load controller B. All as expected. What is not expected is that I do not get $stateChangeSuccess in my B controller.

Why my B isn't getting that $stateChangeSuccess resulting from $state.go from controller A?

Is there any other way for B controller to know that its displayed? (It do not matter which previous state was active, just that state/controller B is active again)

State definitions:

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

Upvotes: 2

Views: 1243

Answers (1)

Radim Köhler
Radim Köhler

Reputation: 123861

I'd say, that the concept is working. There is a working example

And this events will be fired

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

on any transition like this

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

The only important change I made is URL

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

Check it here

Upvotes: 2

Related Questions