如何在单页角度应用程序中更新Web URL [英] How can I update web url in single page angular applications

查看:88
本文介绍了如何在单页角度应用程序中更新Web URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用angularjs将应用程序开发为单页面应用程序。但是对于每个按钮点击和锚标签,可以看到不同的视图,就好像页面被导航到另一个页面,但是网址是不变的,每次点击都不会改变,如下所示

localhost:// 12345

但是如果我点击按钮或标签我想要一些与它相关的id被添加到url中,如果我将它复制并粘贴到另一个标签中,它应该打开相同的东西,就像它是不是下面这样的单页

localhost:// 12345 /#abc12



请建议我怎么做。谢谢提前任何帮助..



我尝试过的事情:



我有使用angularjs作为单页面应用程序开发的应用程序。但是对于每个按钮点击和锚标签,可以看到不同的视图,就好像页面被导航到另一个页面,但是网址是不变的,每次点击都不会改变,如下所示

localhost:// 12345

但是如果我点击按钮或
标签我想要一些与它相关的id被添加到url中,如果我将它复制并粘贴到另一个标签中,它应该打开相同的东西好像它不是下面的单个页面

localhost:// 12345 /#abc12

解决方案

你不能改变没有重定向的网址,但你可以更改网址哈希!



你可以简单地使用位置设置器重定向到哈希。该页面不应该重新加载。



您可以使用哈希来存储您喜欢的任何内容:



  //  未经测试但可能有效 
document location .hash = {嗒嗒:X,blah2:Y};
// /////////////////
var hash = document location .hash;
// hash =#{blah:x,blah2:y}
// 您可以使用json或其他任何内容添加并使用该对象来保存页面状态


I have an application being developed as a single page app using angularjs. But for every button clicks and anchor tags different views are made visible as if the page is navigated to another page but web url is constant not gets changed for each clicks as follows
localhost://12345
but if i click on button or tag i want some id related to it to be added to url and if i copy and paste it in another tab, it should open the same thing as if it is not a single page like below
localhost://12345/#abc12

Please suggest me how to do it.Thanks in advance for any help..

What I have tried:

I have an application being developed as a single page app using angularjs. But for every button clicks and anchor tags different views are made visible as if the page is navigated to another page but web url is constant not gets changed for each clicks as follows
localhost://12345
but if i click on button or
tag i want some id related to it to be added to url and if i copy and paste it in another tab, it should open the same thing as if it is not a single page like below
localhost://12345/#abc12

解决方案

You cannot change the url without redirecting, but you can change the url hash!

You can simply use the location setter to "redirect" to a hash. The page shouldn't reload.

You can use the hash to store whatever you like:

//not tested but might work
document.location.hash = "{blah:x,blah2:y}";
///////////////////
var hash = document.location.hash;
//hash = "#{blah:x,blah2:y}"
//You can add with json or whatever and use that object to save the page state


这篇关于如何在单页角度应用程序中更新Web URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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