2015-12-15 29 views
1

如何禁用更改$位置或重新加載,如果我的形式仍然是無效的,$位置,或在上傳文件禁用回來,重新加載/刷新,改變位置/ URL

windowElement.on('beforeunload', function(event) { 
    if(form === false) { 
    event.preventDefault(); 
    } 
}); 

我也嘗試

$scope.$on('$locationChangeStart', function(event) { 
    if(form === false) { 
    event.preventDefault(); 
    } 
}); 

$scope.$on('$locationChangeSuccess', function(event) { 
    if(form === false) { 
    event.preventDefault(); 
    } 
}); 

所有的人都沒有工作

回答

0

要直接觀看$rootScope,嘗試這樣的事情在你運行阻滯:

$rootScope.$on('$locationChangeStart', function(event, newUrl, oldUrl) { 
    console.log(newUrl); // http://localhost:3000/#/articles/new 
    console.log(oldUrl); // http://localhost:3000/#/articles 
    event.preventDefault(); // This prevents the navigation from happening 
}); 

AgnularJS docs

路線改變(和觸發它的$位置的變化)通過調用事件的preventDefault方法可以阻止 。有關事件對象的更多詳細信息,請參閱 $ rootScope.Scope。從this blog

採取

代碼