What is the difference between [removed] and $loca

2019-05-10 21:33发布

问题:

In MVC angularJS application, how can I redirect to MVC page.

I tried below two options

First

// It doesn't work
$location.path("/MyPage1");

Second

 //It works
 window.location = "/MyPage1";

Please suggest best way to redirect and why ?

REMEMBER : I am not using angularJs Routing.

回答1:

Comparing $location to window.location official doc clearly stated

see the section at this location

seamless integration with HTML5 API

window.location: no

$location: yes (with a fallback for legacy browsers)

and more



回答2:

Both do have their own merits. They are clearly described in the official docs as mentioned by @Mohammad. So depending on the circumstances choose any of the either :

Use $location : When you do not require a full page reload when the browser URL is changed, wants to avail the angular internal life-cycle benefits and where you don't need to support old legacy browsers.This might be useful if your destination is just a variation on the current URL, so that you can take advantage of $location helper methods. E.g. we ran $location.search(..., ...) to just change value of a querystring paramater.

Use native window location : When you need to change the URL and reload the page or navigate to a different page, please use a lower level API: window.location.href or when you want to work with row level object properties that you can directly modified. i.e like Force reload window.location.reload().