简体   繁体   English

如何在不重新加载页面的情况下将浏览器地址栏中的 URL 更改为 angularJS

[英]How to change URL in browser address bar with angularJS without reloading the page

I am new to angularJS, I'm working on a project.我是angularJS的新手,我正在做一个项目。 I'm calling an angular function on ng-click.我在 ng-click 上呼叫 angular function。 the function is working fine but URL is not changing in the browser address bar while if I change the function in pure javascript and call the function. it changes the URL in the browser address bar without page reloading. function 工作正常但 URL 在浏览器地址栏中没有更改,而如果我在纯 javascript 中更改 function 并调用 function。它会更改浏览器地址栏中的 URL,无需重新加载页面

function pushAdd(page) {
    // alert("page");
    window.history.pushState("", "", "/account/" + page);
}

in angular function, I'm calling js function named pussAdd.在 angular function 中,我正在调用名为 pussAdd 的 js function。 when I use alert function within pushAdd() function it shows alert with page name.当我在 pushAdd() function 中使用警报 function 时,它会显示带有页面名称的警报。 but the URL is not changing.但 URL 没有改变。

$scope.changeUrl = function (page) {
    /*my code which is working fine here*/
    pushAdd(page);
}

Use $location.path() to change URL in Angular way :使用$location.path()Angular 方式更改 URL :


angular.directive(
    'fooBar',
    [
        '$location',
        ($location) =>
            {
                restrict: 'EA',
                link: (scope) =>
                    scope.changeUrl = (page) => $location.path("/account/" + page)
            }
    ]
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM