当使用浏览器的后退没有更新的角度$位置 [英] Angular $location not updating when browser back is used

查看:230
本文介绍了当使用浏览器的后退没有更新的角度$位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人碰到这个问题上运行?没有关于它更提到,而其他职位的性质不同。这似乎是不一致的W¯¯有关浏览器的行为/文档。


  

同步的网址与浏览器,当用户


  
  

      
  • 更改地址栏。

  •   
  • 单击后退或前进按钮(或单击某个历史记录链接)。

  •   
  • 点击一个链接。

  •   

  
  

https://docs.angularjs.org/api/ng/service/ $#位置!


会发生什么:

这是一个观察者对位置,以帮助调试:

  //监听在相关范围$事件
$ rootScope。在$('locationChange',函数(事件数据){
    的console.log(数据);
});//调用locationChange看随时在页面加载
。$ $范围发出('locationChange',$位置$$路径。);

下面的路由:

  $ routeProvider
    。当('/指南',{
        templateUrl:意见/ guide.html',
        控制器:'GuideController',
        controllerAs:引导
    })
    。什么时候('/', {
        templateUrl:意见/ about.html',
        控制器:'AboutCtrl',
        controllerAs:关于
    })
    。除此以外({
        redirectTo:'/'
    });


解决方案

如何解决这样的:

  VAR流行= 0;window.onpopstate =函数(事件){
    如果(document.location.pathname ==='/'和;&放大器;弹出→1){
        流行= 0;
        document.location =的http://本地主机:9000 /';
    }
    流行++;
};

Has anyone run across this behaviour? There isn't much mentioned about it, and the other posts are different in nature. This appears to be inconsistent w/ documentation about browser behaviour.

Synchronizes the URL with the browser when the user

  • Changes the address bar.
  • Clicks the back or forward button (or clicks a History link).
  • Clicks on a link.

https://docs.angularjs.org/api/ng/service/$location#!

What happens:

This is a watcher to on location to help debug:

// listen for the event in the relevant $scope
$rootScope.$on('locationChange', function (event, data) {
    console.log(data);
});

//Call locationChange watch at anytime the page is loaded 
$scope.$emit('locationChange', $location.$$path);

Here's the routing:

$routeProvider
    .when('/guide', {
        templateUrl: 'views/guide.html',
        controller: 'GuideController',
        controllerAs: 'guide'
    })
    .when('/', {
        templateUrl: 'views/about.html',
        controller: 'AboutCtrl',
        controllerAs: 'about'
    })
    .otherwise({
        redirectTo: '/'
    });

解决方案

How I solved this:

var pop = 0;

window.onpopstate = function(event) {
    if (document.location.pathname === '/' && pop > 1) {
        pop = 0;
        document.location = 'http://localhost:9000/';
    }
    pop++;
};

这篇关于当使用浏览器的后退没有更新的角度$位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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