Controller of a child state is called twice in AngularJS
What are the possible reasons that may cause a controller of a child state to be called more than once ?
I checked all the open issues and questions and couldn't find an answer to my question.
having it declared in the routeProvider and also having an ng-controller directive in the DOM ?
Hi marcghorayeb,
Thanks for your reply. i double checked it. I have declared only once. i.e., in my html only.
Ranadheer
You'll need to prepare a plunkr that exhibits the issue. Then we can take a look and verify if its a bug or just use error.
I have the same problem (though I can't guarantee it has the same cause). In my case I'm trying to integrate a jQuery lightbox-plugin (fancybox). I'm probably doing something wrong, but I've narrowed it down the following plunkr (without the plugin): http://plnkr.co/edit/6jiK46hLo7SEmYQddMtP?p=preview When I alternate between the two states, the controller of the grandchild state fires first once, then twice, then thrice etc.
[edit]$destroying the scope of the ui-view as it's removed seems to solve my problem. Yay.[/edit]
@towr $destroying the scope sounds like the right approach. The extra controller initializations were happening in $stateChangeSuccess listeners. If you keep around all the scopes you've ever made for state2, then there will be N-1 event handlers lying around (where N is the number of times you've transitioned into state2). By destroying the scopes once they're no longer necessary, you're effectively unregistering those event listeners.
@mfield have we always had to destroy the scopes? or is this a regression?
@timkindberg It took a little thinking, but it looks like the Plunkr is using onEnter/onExit as a weird replacement for the template property.
I forked the Plunkr and made it do a similar thing without invoking $compile. The multiple initialization problem went away.
My conclusion is that ui-view adds/removes elements to the DOM and does a fine job of calling $scope.$new and $scope.$destroy. @towr's onEnter/onExit is adding/removing elements from the DOM, but not making the right calls to $scope.$new and $scope.$remove.
If ui-view wanted to be a little more paranoid, you might be able to set scope: true on the directive definition object and listen for itself to get removed to do more cleanup. That might cause its own set of problems though.
@mfield Yes, it is a weird replacement for the template property, but I don't actually have the control over the dom-elements being injected, so a replacement was what I needed. I whittled down the problem for the plunkr, but in my use-case the fancybox plugin creates and removes the dom-elements. So that's why I didn't (and don't think I can) use the template property. (It's along the same lines as this question in the faq, which also uses onEnter )
I think it's fair enough that some extra steps are needed for a workaround like this. And now that I know I should create a new scope and at the end destroy it myself, things work as expected.
@mfield thank you so much for looking into this!!