如何在angularjs中单击后退按钮时更改URL而不重新加载页面?

时间:2015-06-12 05:39:44

标签: angularjs url routes back-button route-provider

我正在为我的应用程序使用angularjs。它包含新闻源。如果用户需要查看特定新闻,他必须点击该特定新闻,它将重定向到该新闻。在查看特定新闻后,如果用户想要返回新闻源(上一页),则必须单击浏览器后退按钮。因此,在这种情况下,如果用户单击浏览器后退按钮,则新闻源页面将再次重新加载。但是,我不想这样。我想禁用重新加载并将用户带到他以前的地方。我在这个问题上浏览了很多,但没有给出绝对的解决方案。拜托,帮帮我吧。

2 个答案:

答案 0 :(得分:0)

你可以采取一个锚点链接和锚点的点击事件 javascript函数

window.window.history.back(); 

这是它的html代码

   <input type="button" id="alnkBack"  onclick="window.window.history.back();" value="Back" class="button" />

答案 1 :(得分:0)

当您返回时,将触发先前的路线并重新加载数据。如果要阻止重新加载,请在其间放置服务并缓存数据。

 $routeProvider.
  when('/loadFeeds',{
    controller:'LoadFeedCtrl',
    templateUrl:'pages/feeds.html',
    resolve:{
        initData : function(FeedService){
          return $q.all(
                    {
                        data: FeedService.load() 
                    }
                 );
        }
    }
  })

在您的FeedService中,不要进行http调用,而是尝试查看数据已经可用

.service('FeedService',function($q,$timeoute){
    this.feeds=[];
    this.load = function(){
           var deferred = $q.defer();
               $timeout(function(){
                  if(feeds.length===0){
                    this.feeds.push({title:'First','id':1})
                    this.feeds.push({title:'Second','id':2})
                    this.feeds.push({title:'Third','id':3})
                    this.feeds.push({title:'Fourth','id':4})
                  }
                  deferred.resolve(this.feeds);
               },2000);
          return deferred.promise;
    };
})

超时服务可以用$ http或$ resource替换。我使用超时来模拟延迟