简体   繁体   English

使用AngularJS / Javascript仅禁用浏览器后退按钮导航(不链接应用程序中的链接)

[英]Disable Only Browser Back Button navigation(not links in app) with AngularJS/Javascript

I am working on a SPA using AngularJS. 我正在使用AngularJS开发SPA。 I want to navigate through application states using links in application and I want to prevent the same navigation when back button is clicked. 我想使用应用程序中的链接浏览应用程序状态,并且当单击后退按钮时,我想阻止相同的导航。

All the solutions I found including $locationChangeStart in angularJs prevent navigation when I click links in my app also. 当我也单击应用程序中的链接时,我发现的所有解决方案(包括angularJs中的$ locationChangeStart)都无法导航。

Please provide a solution which prevents only browser back button navigation and allows navigation through clicking link in application. 请提供一种仅阻止浏览器后退按钮导航并允许通过单击应用程序中的链接进行导航的解决方案。

The easiest way to do this would be to have a mapping of transitions from current application state (url) to valid next application states (urls) and only preventDefault iff the transition from the current state to the one desired by $locationChangeStart isn't valid. 最简单的方法是从当前应用程序状态(url)到有效的下一应用程序状态(urls)的转换映射,并且仅当从$locationChangeStart stateChangeStart从当前状态到所需状态的转换无效时才阻止Default 。

If you go about it like this, you not only prevent the user from using the back button but also from putting undesireable urls in the browser to create invalid application state and possibly harm your application. 如果这样处理,不仅会阻止用户使用后退按钮,而且还会在浏览器中放置不良网址,从而造成无效的应用程序状态并可能损害您的应用程序。

I suggest you don't use some "on history change history.forward" method like linked in the other answer, it's the worst UX possible, make it transparent to the user. 我建议您不要在其他答案中使用链接中的“ on history change history.forward”方法,这是最糟糕的UX,使它对用户透明。

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

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