window.location和$ location.path有什么区别? [英] What is the difference between window.location and $location.path?

查看:709
本文介绍了window.location和$ location.path有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MVC angularJS应用程序中,如何重定向到MVC页面。

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

我在下面尝试了两个选项

I tried below two options

第一次

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

第二

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

请建议最佳重定向方式,为什么?

Please suggest best way to redirect and why ?

请记住:我没有使用angularJs路由。

REMEMBER : I am not using angularJs Routing.

推荐答案

两者确实有自己的优点。在@Mohammad提到的官方文档中清楚地描述了它们。因此,根据具体情况选择以下任意一种:

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 :

使用 $ location :当浏览器URL不需要重新加载整页时已更改,希望利用角度内部生命周期优势,以及您不需要支持旧版旧浏览器。如果您的目标只是当前网址的变体,这可能很有用,这样您就可以利用$位置辅助方法。例如。我们运行$ location.search(...,...)来改变查询字符串参数的值。

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.

使用原生的窗口位置 :当您需要更改URL并重新加载页面或导航到其他页面时,请使用较低级别的API:window.location.href,或者您希望使用可以直接修改的行级别对象属性。即强制重载window.location.reload()。

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().

这篇关于window.location和$ location.path有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆