AngularJS:避免更改浏览器位置 URL 以阻止用户为错误页面添加书签?

编程入门 行业动态 更新时间:2024-10-27 18:22:34
本文介绍了AngularJS:避免更改浏览器位置 URL 以阻止用户为错误页面添加书签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我有一个表单需要提交,如果出现问题,我想显示一个错误页面,我有一个控制器和视图,但我希望浏览器位置文本框不要更改以避免用户来自为错误页面添加书签.

I have a form that I need to submit, if something goes wrong I would like to show an error page, I have a controller and view for this but I would like for the browser location textbox not to change to avoid the user from bookmarking the error page.

所以我会有一个名为/Items 的页面

So I would have a page called /Items

用户提交时出现错误,所以我想向用户显示此页面 (itemsError.html),但我不想让用户将/ItemsError 加入书签

the user submits and there was an error so I would like to show this page (itemsError.html) to the user but I don't want to allow the user to bookmark /ItemsError

如果我插入 routeprovider,那么浏览器位置栏将更新为/ItemsError 之类的内容,稍后用户可以为该页面添加书签,但该页面是一个动态页面,应仅根据表单提交结果.

If I plugin into the routeprovider then the browser location bar is going to update with something like /ItemsError and at a later date the user could bookmark the page, but this page is a dynamic page and should only be shown depending on the result of the form submission.

Angular 支持此类事情的最佳实践是什么?

What is Angular's best practice for supporting something like this?

谢谢

推荐答案

config.js

$routeProvider.when('/', {
  template: '/views/home.html',
});

index.html

<!doctype html>
<html lang="en" ng-controller="app">
<head>
</head>
<body>
<ng-include src="page"></ng-include>
</body>
</html>

controllers.js

controller('app', ['$scope','$route',function($scope,$route) {
  $scope.$on("$routeChangeSuccess",function($currentRoute,$previousRoute){
    $scope.page = $route.current.template;
  });
}]).

这将处理您的一般路由.现在您可以根据逻辑动态交换控制器中的部分.在您的示例中,如果表单未成功完成,您的错误回调可以即时更改部分:

This will handle your general routing. Now you can dynamically swap partials in your controllers based on logic. In your example, if the form doesn't successfully complete, your error callback can just change the partial on the fly:

controller('form', ['$scope','$route',function($scope,$route) {
  $scope.submit = function(){
    something.get().$then(
       function( value ){$scope.page = '/views/successPage.html/';},
       function( error ){$scope.page = '/views/errorPage.html/';}
    )
  }
}]).

这篇关于AngularJS:避免更改浏览器位置 URL 以阻止用户为错误页面添加书签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-21 17:04:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1007541.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:书签   浏览器   错误   位置   页面

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!