如何在不刷新angularjs的情况下返回上一页? [英] How to go back to previous page without refreshing in angularjs?

查看:33
本文介绍了如何在不刷新angularjs的情况下返回上一页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个内置路由的 AngularJS 应用程序.它工作正常,一切正常.我有 2 个控制器.在第一个控制器中,当我单击按钮时,我有一个按钮,第二个控制器将运行.

当我尝试返回或 history.back() 时,第一个控制器再次执行.

如何预防?

解决方案

如果您将控制器编写为特定于页面的控制器,则当任何页面从一个页面重定向到另一个页面时,angularjs 将始终执行控制器.

如果您的应用程序是 SPA,那么您可以通过在声明 ng-view 之前指定控制器来重写(这不是一个好习惯).as

 

<div ng-view>

现在它将保存您的所有数据,直到应用程序关闭.但是,您应该注意缓存.

I currently have an AngularJS application with routing built in. It works and everything is ok. where i have 2 controller. in first controller i have a button when i click button 2nd controller will run.

when i try to go back or history.back() the 1st controller is again executing.

how to prevent it?

解决方案

angularjs will always execute controllers when ever page is get redirected from one page to another page if you written controller as page specific.

If your application is SPA then you can rewrite by specifying a controller before declaring ng-view (its not a good practice).as

    <div ng-controller="controller as cont">
       <div ng-view>
       </div>
     </div>

now it will hold all your data until the application get closed.but, you should aware of cache.

这篇关于如何在不刷新angularjs的情况下返回上一页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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