Good day. Help please, there is a huge SPA using the old version angular 1.4.8

Throughout the project, to navigate between the "pages", the ToUrl function is used with the following code:

var ToUrl = function (relativeUrl) { var mainContainerElement = window.angular.element("#MainContainer") var rootScope = mainContainerElement.injector().get("$rootScope"); var ngLocation = rootScope.ngLocation; if (rootScope && ngLocation) { rootScope.historyPushCount = 1; rootScope.$evalAsync(ngLocation.url(relativeUrl)); } } }; 

In all places where this function (ToUrl) is called everything is fine. In addition to one service. For some reason, angular sends another request to the server with the same url to which the transition is performed by the ToUrl function. It turns out 2 requests in a row.

Using the debugger, I found the function fireUrlChange, in which there is a lastHistoryState === cachedState check and this check returns false. Because of this, angular creates another query. In the belt, cachedState is always null, but for the time the function is triggered, the number 2 for some reason appears there.

Here is the code for the fireUrlChange function:

 function fireUrlChange() { if (lastBrowserUrl === self.url() && lastHistoryState === cachedState) { return; } lastBrowserUrl = self.url(); lastHistoryState = cachedState; forEach(urlChangeListeners, function(listener) { listener(self.url(), cachedState); }); } 

Help please, how to find the reason for changing the parameter cachedState (or lastHistoryState)?

  • one
    The behavior described by you is very similar to the forced addition of a hash to the link after completing the transition to the page. Usually such "tricks" are performed for positioning on the specified anchor in the page. - Nikolay
  • one
    Calling fireUrlChange with a deuce in the state is the normal behavior of the angular and such a call should not reload the page. Perhaps the problem is that somewhere in the code the state of $ browser.state changes too early - Nikolay
  • @Nikolay, could you drop some detailed information how this fireUrlChange works? I can’t dig anything out anywhere, but I don’t understand it by the code of the big picture :( - UserTest013
  • one
    I can not provide detailed documentation, because never met like that. My statement is based on reading the source code of the angular itself. - Nikolay

0