简体   繁体   English

使用AngularJS和routeparams的后退按钮循环

[英]Back button loop with AngularJS and routeparams

Here's what I wish to achieve: 这是我希望实现的目标:

  1. User is on url example.com/2 用户位于网址example.com/2上
  2. User goes to url example.com/2d <- this is not valid url 用户转到网址example.com/2d <-这是无效的网址
  3. User is fowarded to example.com/2d/404_error 用户偏向于example.com/2d/404_error
  4. User clicks browser "back" button and lands on example.com/2 用户单击浏览器的“后退”按钮,然后进入example.com/2

Here's what I have a problem with 这是我的问题

  1. User clicks browser "back" button and lands on example.com/2d 用户单击浏览器的“后退”按钮并进入example.com/2d
  2. User is again automatically forwarded to example.com/2d/404_error 再次将用户自动转发到example.com/2d/404_error

I have created infinite back button loop :( 我创建了无限后退按钮循环:(

In my app.config I have 在我的app.config中

.when('/:gameId/404_error', {
    templateUrl: 'views/page_not_found.html'
})

.when('/:gameId', {
    controller: 'game',
    templateUrl: 'views/gamePage.html'
})

It is called out by app.factory when data is not fetched 未获取数据时由app.factory调用

}, function(response) {
    // something went wrong
    $location.path("/" + id + "/404_error");
    return $q.reject(response.data);
});

I have tried the "otherwise" method, but using routeparams renders it useless. 我尝试了“其他”方法,但是使用routeparams使其变得无用。

You can test it out here: 您可以在这里进行测试:

http://vivule.ee/2 <- working url http://vivule.ee/2 <-工作网址

http://vivule.ee/2d <- not working url http://vivule.ee/2d <-无效的网址

window.history.go(-2) window.history.go(-2)

This method is pure javascipt you can try to use $window.history.go(-2) in angularjs 此方法是纯Javascipt,您可以尝试在angularjs中使用$ window.history.go(-2)

Good works! 干得好!

Fixed it with ng-switch and ng-include 用ng-switch和ng-include修复了它

In the controller, when data is not loaded I added: 在控制器中,当未加载数据时,我添加了:

$scope.page_not_found = true;

And to the template I added: 在我添加的模板中:

<div ng-switch on="page_not_found">
   <div ng-if="page_not_found" ng-include="'views/page_not_found.html'"></div>
</div>

Everything happens in one .html template. 一切都发生在一个.html模板中。 I hide the main content and import the 404 page with ng-include, this solution seems to be faster as well. 我隐藏了主要内容,并使用ng-include导入404页面,该解决方案似乎也更快。

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

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