Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to access event.currentScope in $stateChangeSuccess #1005

Closed
ncuillery opened this issue Apr 6, 2014 · 3 comments
Closed

Unable to access event.currentScope in $stateChangeSuccess #1005

ncuillery opened this issue Apr 6, 2014 · 3 comments

Comments

@ncuillery
Copy link
Contributor

I searched a way to get the scope associated to the current state. I used the $stateChangeSuccess event who expose the event.currentScope... but I found a strange behavior when logging the event.currentScope :

$scope.$on('$stateChangeSuccess', function(event) {
    console.log('event ', event);
    console.log('event.currentScope', event.currentScope);
});

The first console.log displays the whole event object, and I can see what I need under the property currentScope.
But if I try to log specifically event.currentScope, I get an another scope (the $rootScope, I think).

With console.log or anything else, I can't get the currentScope.

See it in action here : http://jsfiddle.net/Cuicui74/HxrSd/11/ (open console).

@ncuillery
Copy link
Contributor Author

I did some debugging into the $broadcast method and I think I understood the problem:

Chrome Dev Tools (and Firefox too) displays directly the Scope object in the second log (console.log(event.currentScope)) and it's the good one (the one who has his method $on used).

But with the first log (console.log(event)), it only keeps a reference and displays "Object" for the currentScope property. The value is displayed only when the user expand it by clicking on the arrow: at this time, in the $broadcast method, the "broadcast loop" is done and the currentScope property is the deepest scope in the tree, it has nothing to do with the event (or coincidentally has).

I think crazy things can happened when using a $timeout function referencing the event.currentScope for the same reason so it's a real bug. But it concerns AngularJS, not the UI-Router.

I'll make a PR.

@nateabele
Copy link
Contributor

To be clear, there is no explicit support or notion of a 'current scope' when it comes to state events. There are a number of reasons for this, including the fact that a state can be attached to to any number of scopes from zero to infinity (because a state can have any number of views). Secondly, it crosses the abstraction boundary between view management and the state machine.

@ncuillery
Copy link
Contributor Author

Thanks for explanation @nateabele.

Here is the end of the story:

I submit a PR about this problem. A correction arises from this, consisting in setting event.currentScope to null at the end of the broadcast loop. No more misleading behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants