Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular JS $on "$locationChangeStart" triggered twice when event.preventDefault()

I'm trying to listen to the $on "$locationChangeStart" event in Angular JS and prevent the event from happening under certain circumstances. As soon as I do event.preventDefault(), the $on "$locationChangeStart" event gets fired again. Is that a Angular JS Bug?

$rootScope.$on "$locationChangeStart", (event, next, current) ->
  if doNotLeave
    event.preventDefault()

Thanks in advance, Steff

like image 289
Stefan Avatar asked Mar 27 '14 11:03

Stefan


1 Answers

Okey, I've seen a very similar situation and found one possible cause. Angular (or our custom route-segment routing, not sure which) will sometimes trim a URL after routing processes it. Try logging next and current and see, what routes exactly you are dealing with. In our case the change was from this http://wof/Admin#/someRoute to http://wof/Admin#/someRoute/ (notice added slash). After we accounted for this - it worked ok.

like image 72
Ivan Koshelev Avatar answered Sep 23 '22 09:09

Ivan Koshelev