hitesh_noty
hitesh_noty

Reputation: 385

why controller of old state is getting executed even after state is changed successfully

Suppose there are two states, A and B.

.state("A", {
  url:'/A',
  template: '<div></div>',
  controller: 'ACtrl',
}).state("B", {
  url:'/B',
  template: '<div></div>',
  controller: 'BCtrl',
})

And then there are two controllers:

.controller('ACtrl',function($state){
    $state.go('B');
    console.log('Why is this getting executed?');
  })
.controller('BCtrl',function($state){

  })

Now if am going to url-> /A why the console.log statement is getting executed even after state is changed successfully?

That Controller should stop working. Please let me know where am i going wrong. I just started exploring UI-Router and AngularJs.

Upvotes: 1

Views: 431

Answers (2)

dfsq
dfsq

Reputation: 193301

That Controller should stop working. Please let me know where am i going wrong.

No it should't. There are only few statements/constructs that would act as a code flow control and prevent subsequent code execution. Those are

You don't have anything like this, hence your code gets executed. As expected.

Upvotes: 1

Kashif Mustafa
Kashif Mustafa

Reputation: 1182

If you implement $state.go in initialization of any controller , than all the methods of that controllers must be called (its the default behavior of controller even if the state are changed). Although it does not make any sense for implementing $state.go in initialization of any controller. But for the sake for understanding, you can check by doing different things. I assumed you got my point.

Upvotes: 0

Related Questions