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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?