简体   繁体   English

$ locatioChangeStart触发多次

[英]$locatioChangeStart fires multiple times

basically, I'm trying to make some array which should have history of visited pages in my app. 基本上,我正在尝试制作一些数组,该数组应在我的应用程序中包含已访问页面的历史记录。

$rootScope.$on('$locationChangeStart',
    function (event, next, current) {

    historyArray.push($location.path());

    console.log("history", historyArray);
});

at the begining it looks ok, I mean [ "/page1", "/page2"], but then it starts to multiply the "ChangeStart" effect to ie ["/page1", "/page2", "/page3", "/page3", "/page4", "/page4", "/page4"] etc. 一开始看起来不错,我的意思是[“ / page1”,“ / page2”],但是随后它开始将“ ChangeStart”效果乘以即[“ / page1”,“ / page2”,“ / page3”, “ / page3”,“ / page4”,“ / page4”,“ / page4”]等。

any ideas how to prevent it? 任何想法如何预防呢?

edit. 编辑。 this is just an example, i need $locationChangeStart for some ngDialog modals and other complicated things, but I'm facing similar sutation (like opening 5 modals at the same time) 这只是一个例子,我需要$ locationChangeStart用于某些ngDialog模态和其他复杂的东西,但是我面临着类似的缝合(例如同时打开5个模态)

You can just add a conditional to check if the location actually changed and an indexOf to make sure the page doesn't exist within the array. 您可以只添加一个条件以检查位置是否实际更改,还可以添加indexOf以确保该页面在数组中不存在。

$rootScope.$on('$locationChangeStart',
    function (event, next, current) {
        if(next !== current && historyArray.indexOf(current) === -1) {
            historyArray.push($location.path());
        }
    }
);

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

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