ui-sref cause redundant state transition
I noticed that whenever ui-sref is used, a state is transitioned to twice. Upon examining the source code, it looks like the issue is caused by the fact that $StateRefDirective not only generates a href but also binds onclick event. By removing one of those, the issue is solved.
Is this a bug or by design? Given that the built-in ng-href only generates href, is there a reason ui-sref has to bind the onclick event?
If you look here, you'll notice that ui-sref suppresses the default click behavior, so for instances where you're clicking the link, the transition is handled by $state.go(), not by the link changing the URL.
Hmm, you're right. Still, I have that issue and removing either the href or onclick stops it. Let me see if I can reproduce it in a plunker.
AFAIK, you can't disable the browser's response to href in html.
This plunker demonstrates the problem. Interestingly, the issue only happens when the function inside resolve runs asynchronously. I include both cases in that plunker, just comment out the first or second case and notice the difference when clicking the Next link.
When I replace e.preventDefault() with return false, the problem disappears. But... only when jquery is referenced. If jquery is not used, the problem persists.
Examining jquery source, return false also causes e.stopPropagation() to be invoked. Adding e.stopPropagation() after e.preventDefault() fixes the issue regardless of whether jquery is used or not (see this plunker). But stopping propagation is obviously not a good idea.
I have no idea why propagation has a part in this (again, this issue occurs in both the simple plunker provided as well as my production app). Besides, back to the original question, why should ui-router generates href and onclick at the same time, why not just href like ng-href?
Because states don't have to have URLs.
Maybe the answer is to only do one or the other depending on if a URL is defined.
Strongly disagree. One of the core concepts of this library is bridging the gap between new-school applications and standard Web conventions, which is what this enables.
I'm not exactly sure why you think @timkindberg's suggestion defeats that purpose? I don't see how a user can possibly be affected by that change. (Doesn't mean I'm not interested in learning about the root cause & solving it.)
Sorry, I misread it. I'll try and figure out a solution for this as soon as I can. Probably this weekend.
I just submitted a PR, please take a look. (Sorry about multiple references in this thread; the PR has only 1 commit however.)
Please see my comments on the PR. Unfortunately it doesn't really satisfy the requirements. I'll have to add some additional test coverage to uiSref so we don't have any regressions.
@buunguyen Thanks for a quick-fix. I'll use yours until offical fix is applied. Since I have some ajax queries on my states' resolve, I don't want to fetch them twice. :)
@ubenzer you're welcome. I'm also using the stopPropagation() hack in my app. Hope @nateabele have a chance to take a look at this soon.
I also ran into this issue. I couldn't make the event.stopPropagation() hack work in my use case. Here's my workaround:
var off = scope.$on('$stateChangeStart', foo);
Calling off will remove the listener. So just call off in foo. Then it doesn't matter if the state change transition starts twice.
Obviously, there are lots of instances where this wouldn't work.
Looking forward to the real fix in ~ 0.4.0!
Thanks @buunguyen - I just needed this line: if (isForm || nav) return;
Could we get this merged? Double triggering of state is a no-win.
I ran into similar problem. A state transition is performed twice. It happens when my ui-sref doesn't include all of the state parameters. My intention is to have optional parameter. The first redirection supplies null as a value of the missing parameter and then the second redirections supplies "" in place of null. I know that ui-router doesn't really support optional parameters but this behaviour is odd. Maybe it will somewhat helpful for finding the cause
I can confirm that the (as stated by @lubiluk) is with the ui-sref not including all of the state parameters. In my case I was trying to rely on parameters from parent routes already being resolved.
+1 @lubiluk
The first redirection supplies null as a value of the missing parameter and then the second redirections supplies "" in place of null.
I am using the master branch which supposedly should support optional parameters. However, I get the same behavior with null being passed once, and "" the second time. I am working around this by passing in a "" instead of omitting the optional parameter and checking for "" in my controller.
Still seeing an issue with the empty optional params that @lubiluk mentioned...would be nice to see a fix without a workaround. Not the end of the world as there is a workaround as @kbdaitch brings up, markup ends up looking like <li ui-sref="smart({id: ''})" >