防止垃圾邮件历史记录中的搜索更改

maa*_*nus 7 routes hyperlink angularjs

我正在使用类似的链接#!/OrderList?id=123,它显示所有订单的列表和订单123的更多细节.使用reloadOnSearch=false和观看$routeUpdate它工作正常,除了一件事:所有这些链接都被放入浏览器历史记录中,而我更喜欢那里只有一个这样的链接.例如,而不是

#!/OrderList?id=123
#!/OrderList?id=124
#!/OrderList?id=125
#!/AnotherList?id=678
#!/AnotherList?id=679
Run Code Online (Sandbox Code Playgroud)

只是每个组的最后一个成员,即

#!/OrderList?id=125
#!/AnotherList?id=679
Run Code Online (Sandbox Code Playgroud)

我知道$location.replace(),但是当通过一个链接发生变化时我无法看到它放在哪里.我试图将它放入$scope.$on("$routeUpdate", ...),但它没有做任何事情,可能是因为当路线已经改变时为时已晚.

我既不使用router-ui也不使用HTML5模式(只是简单angular-route).


我担心,我不清楚我坚持使用href而不是自定义处理程序.我希望链接能够与鼠标中键和书签以及所有内容一起使用.结合ng-href并可ng-click能做我想要的,但我找到了一个使用普通链接的简单解决方案.

maa*_*nus 0

我对任何答案都不满意,经过相当多的调试后,我找到了这个解决方案:

.run(function($rootScope, $location) {
    var replacing;

    $rootScope.$on("$locationChangeStart", function(event, newUrl, oldUrl) {
        if (oldUrl === newUrl) return; // Nobody cares.

        // Make urls relative.
        var baseLength = $location.absUrl().length - $location.url().length;
        newUrl = newUrl.substring(baseLength);
        oldUrl = oldUrl.substring(baseLength);

        // Strip search, leave path only.
        var newPath = newUrl.replace(/\?.*/, "");
        var oldPath = oldUrl.replace(/\?.*/, "");

        // Substantial change, history should be written normally.
        if (oldPath !== newPath) return;

        // We're replacing, just let it happen.
        if (replacing) {
            replacing = false;
            return;
        }

        // We're NOT replacing, scratch it ...          
        event.preventDefault();

        // ... and do the same transition with replace later.
        $rootScope.$evalAsync(function() {
            $location.url(newUrl).replace();
            replacing = true;
        });
    });
})
Run Code Online (Sandbox Code Playgroud)