简体   繁体   English

$ window.location更改后AngularJS后退按钮不起作用

[英]AngularJS back button not working after $window.location change

I am using the md-grid-tiles as links to new pages on a site. 我使用md-grid-tiles作为网站上新页面的链接。 To give some context I have an AngularJS 1 app that is a single-page app using ng-view. 为了提供一些背景信息,我有一个AngularJS 1应用程序,它是使用ng-view的单页应用程序。 So my links are all formatted with #page. 因此,我的链接都使用#page格式化。 Every tile is given an attribute called link and I have attached this function: 每个图块都有一个名为link的属性,我附加了以下功能:

$scope.direct = function(tile) {
    $window.location.href = tile.link;
}

to the ng-click portion on my md-grid-tile: 到我的md-grid-tile上的ng-click部分:

<md-grid-tile ng-repeat="tile in vm.tiles track by $index"
                              md-rowspan="{{tile.span.row}}"
                              md-colspan="{{tile.span.col}}"
                              md-colspan-sm="1"
                              md-colspan-xs="1"
                              ng-class="tile.background"
                              ng-click="direct(tile)">

and the tile.link variable will be '#newpagetogoto' 并且tile.link变量将为'#newpagetogoto'

This works as expected but the back button functionality is not there after I reroute using this method. 这可以按预期工作,但是使用此方法重新路由后,后退按钮功能不存在。 When I press back after going to the new page it just reloads that new view and does not put me back at the home page. 当我进入新页面后按回去时,它只是重新加载了该新视图,而没有使我回到首页。 Is there a way to fix this? 有没有办法解决这个问题?

If I use <a> to link to new views of my site the back button functionality works as expected, but only when I use $window.location.href does it ruin the back button functionality. 如果我使用<a>链接到网站的新视图,则后退按钮功能将按预期工作,但仅当我使用$ window.location.href时,它才会破坏后退按钮的功能。

Example of using <a> : 使用<a>示例:

<a href="#about" onclick="closeNav()">Our Purpose</a>

Any and all help is very appreciated, thanks in advance! 非常感谢您提供任何帮助!

EDIT: Using <a> does not actually keep the back button functionality, it also breaks when routing using <a> link. 编辑:使用<a>实际上并不保留后退按钮功能,使用<a>链接进行路由时也会中断。

You must use $location instead of $window 您必须使用$ location而不是$ window

$location.path(title.link)

Take a look $location documentation: https://docs.angularjs.org/guide/ $location 看一下$ location文档: https : //docs.angularjs.org/guide/ $ location

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

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