呈现404代替angularjs重定向404重定向、angularjs

2023-09-14 23:06:50 作者:漫长

我在寻找一个好的方法来呈现,而不是重定向angularjs 404页404页。我发现有很多解决方案都是关于重定向到另一个页面。这将创建一个问题,如果用户点击浏览器的后退按钮,我将创造另一个重定向到404页。所以我要寻找呈现404页,而不是一个解决方案。

I'm looking for a good approach to render 404 page instead of redirecting 404 page in angularjs. Many solutions I have found is all about redirecting to another page. It will create a problem that if the user click on browser's back button I will create another redirect to 404 page. So I am looking for a solution that renders 404 page instead.

感谢您的阅读,我希望这是可以理解为你们。

Thanks for reading, I hope it's understandable for you guys.

推荐答案

的用法,否则可能是你在寻找的。

Usage of otherwise might be what are you looking for.

angular.module('MyApp', [])
.config(function($routeProvider) {
  $routeProvider.
    when('/', {templateUrl:'/home.html'}).
    // add as many as you want here...
    otherwise({templateUrl:'/404.html'}); // Render 404 view
});

更新:更仔细地阅读OP的问题(不好意思,很早就在这里),我的想后的我有一个替代的解决方案(实际上是两个):

Update: After reading more carefully the OP question (sorry, quite early around here), I think I have an alternate solution (actually two):

1) $范围变量设置为 NG-视图的主UI 隐藏的内容

1) A $scope variable to the main UI of your ng-view that hides the content

这需要 NG-节目在您的视图和解析在您的参数,可以再做一个 $发出其他控制器,以告诉嘿,这家伙打了404,不显示您的视图

This requires ng-show in your view and resolve in your params, then do a $emit to the other controllers in order to tell "Hey, this guy hit a 404, don't display your view"

$routeProvider.otherwise({
  controller: 'masterController',
  resolve: {
    404: function(){
      return true;
    };
  });

angular.module('MyApp', [])
.controller('masterController', function($scope, 404) {
  $scope.isOn404 = 404
  ...
})

// In the view

<div ng-controller="masterController">
   <div ng-hide="isOn404">
    <!-- Actual content -->
   </div>
   <div ng-show="isOn404">
    <!-- 404 Page -->
   </div>
</div>

现在,这需要你有一个主控制器,可以帮助你管理你的UI的其余部分。此外,您最有可能需要做一些编码来处理页面的其余部分,而不是仅仅使用 NG-视图(例如某些控制器,显示当前的标题,正文,等等)。

Now, this requires that you have a master controller that helps you to manage the rest of your UI. Also, you most likely would need to do some coding to handling the rest of the page instead of just using ng-view (e.g. some controllers that show the current header, body, etc).

2)的自定义路由系统

其实我已经这样做了具体项目:你有设置了一个BackURL和FordwardURL的服务;每个 $ onRouteChange 你商店,你去哪里你来自;如果用户将打404,你仍然可以使它通过我原来的例子,但是当用户点击回来,赶上通过AngularJS并呈现实际的后退的页面。在这种情况下,我使用了我工作的公司库,帮助我在移动设备上的路由称为 LUNGO 和图书馆使用时, LAB (LUNGO角桥)。

I actually have done this for a specific project: you have a service that sets up a "BackURL" and "FordwardURL"; each $onRouteChange you store where do you go and where do you come from; if the user is about to hit a 404, you can still render it through my original example, but when the user hits back, catch that through AngularJS and render the actual "Back" page. In this case I'm using a library that helps me with the routing on mobile devices called Lungo and a library that the company I work for uses, the L.A.B (Lungo Angular Bridge).