douguabu8960 2016-10-18 11:11
浏览 654
已采纳

更改URL而不刷新AngularJS中的页面

I am working on a project in which I have requirement to change the URL after a specific page, because there are some credentials in the URL.

I did that by using location.Replace() method. It changed the URL in address bar and reloaded the page, but I don't want that.

For example in my URL abc.com/app/XXXX/#/app/inbox I want to remove XXXX/ on the replacement.

I used location.replace(abc.com/app/#/app/inbox). It replaced the url, but also reloaded the page. I don't want my page to be reloaded on URL change.

  • 写回答

1条回答 默认 最新

  • dt56449492 2016-10-18 12:50
    关注

    In your app.js file add this code. It's adding an extra parameter to $location.path().

    app.run(['$route', '$rootScope', '$location', function ($route, $rootScope, $location) {
        var original = $location.path;
        $location.path = function (path, reload) {
            if (reload === false) {
                var lastRoute = $route.current;
                var un = $rootScope.$on('$locationChangeSuccess', function () {
                    $route.current = lastRoute;
                    un();
                });
            }
            return original.apply($location, [path]);
        };
    }]);
    

    then in your controller call $location.path() as

    $location.path('/users/' +$scope.userid, false);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?