AngularJS:避免更改浏览器位置 URL 以阻止用户为错误页面添加书签? [英] AngularJS: Refrain from changing the browser location URL to stop user from bookmarking an error page?

查看:38
本文介绍了AngularJS:避免更改浏览器位置 URL 以阻止用户为错误页面添加书签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆