The process of doing the project with Angularjs encountered a situation: In the Internet Explorer, when visiting the Site page, click the left and right (back and forward) button in the browser, you need to click two times to correctly jump, but in chrome and other browsers The bug is not reproduced, Later found that the reason is Angularjs page jump code with inappropriate: In the $watch, $on and so on the use of page jump best with $location use.
$scope. $watchCollection (' Parentobj.selection ',function() {$scope. $broadcast (' appidchanged '); }, true); $scope. $on (' appidchanged ',function() {_showmask (); varCurrenturl=$location. Absurl (); varCurrentnewurl=changeurlarg (Currenturl,' AppId ', $scope. parentobj.selection); Console.log (Currenturl+"---"+Currentnewurl); $window. Location.href=currentnewurl; });
The page jumps in the previous code are used:
$window. Location.href=currentnewurl;
Later, instead:
$location =currentnewurl;
Test in the browser does not appear click two times before the page jumps.
Below is a look at the information, summed up the difference between the $location and $window.location.href:
$location:
- Expose the URL of the current address bar so you can
- Gets and listens for the URL.
- Change the URL.
- Synchronize URLs when the following conditions occur
- Change the Address bar
- Click the Back button (or click on the history link) (fixed a bug that clicked two times in IE to return to the previous page)
- Clicked on a link
- A series of methods to get the specific contents of the URL object (protocol, host, port, path, search, hash). FormatDate
1. Compare $location and Window.location
1) Purpose: window.location and $location services allow read and write access to the location of the current browser.
2) API:window.location exposes an unprocessed object with some properties that can be modified directly, while the $location service exposes some jquery-style getter/setter methods.
3) Integration with angular application declaration cycle: $location know the stages of all internal declaration cycles, integrate with $watch, and window.location.
4) seamless integration with HTML5 API: Yes (with a fallback for legacy browsers, is there a compatible means for a lower version of the browser? ), while Window.location is not.
5) know the document root (Docroot) or context in which the app is loaded: Window.location No, Wnidow.location.path will return/docroot/subpath; $location.path ( ) returns the true Docroot.
When should I use $location?
In the app, any need to respond to changes to the current URL, or to change the URL of the current browser
It can't be used for what?
Using $location can make the URL change, but will not refresh the entire page, to refresh the page, with the low-level API $window.location.href .
Angularjs page jump bug under IE