简体   繁体   English

Angular.js-更改URL而不更新当前控制器和模板

[英]Angularjs - Change URL without update current controller and template

i'm developing a front-end like pinterest, when the pin is clicked this should be loading in a modal, no problem here. 我正在开发像pinterest这样的前端,当单击图钉时,应该以模式加载,这里没有问题。

but a requirement is that, the URL should change when the modal is loaded and in the background must keep pins list. 但要求是,加载模式时,URL应该更改,并且在后台必须保留引脚列表。

I can't do it, tried using angular-ui, but when the URL change the ui-view elements are cleaned too 我无法做到,尝试使用angular-ui,但是当URL更改时,也会清除ui-view元素

Here in stackoverflow are many similar questions, but the problem here is that not children URLs 这里有很多类似的问题,但是这里的问题是不是子URL

url1: site.com / list / categorie -> url2: site.com / title url1:site.com/列表/类别-> url2:site.com/标题

How I can change the URL and keep the current contents in the background? 如何更改URL并将当前内容保留在后台?

thanks! 谢谢!

After few days i got it, this code must be in the controller 几天后我明白了,此代码必须在控制器中

var lastRoute = $route.current;
    $scope.$on('$locationChangeSuccess', function(event) {
        if($route.current.$$route.controller == 'modal'){
            $route.current = lastRoute;
        }
    });

this will keep the current ngview HTML, but change the url 这将保留当前的ngview HTML,但更改网址

I found the solution here https://stackoverflow.com/a/12429133/1179213 我在这里找到解决方案https://stackoverflow.com/a/12429133/1179213

You can most likely use HTML history's API. 您很可能会使用HTML历史记录的API。 Check out this post. 看看这篇文章。 Updating address bar with new URL without hash or reloading the page 使用新的URL更新地址栏,而不散列或重新加载页面

Since you are doing this outside of Angular, it should not affect anything but the URL. 由于您是在Angular之外执行此操作,因此它不会影响URL以外的任何内容。

Hope that works! 希望能成功!

您可以使用查询参数通过$location.search$watch -ers突出显示正确的图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM