防止angularjs应用程序中的重定向行为

aga*_*yan 4 javascript angularjs angularjs-directive

当用户尝试单击浏览器后退按钮并且dere是表单上的脏数据时,它会显示确认.

这是角度js控制器代码

   function MyCtrl2($rootScope, $location, $scope) {

    $rootScope.$watch(function () {
        return $location.path();
    },

    function (newValue, oldValue) {
        if ($scope.myForm.$dirty) {
            var a = confirm('do you');
            if (!a) {
                 //how to prevent this redirect
            }
        }
    },
    true);
}

MyCtrl2.$inject = ['$rootScope', '$location', '$scope'];
Run Code Online (Sandbox Code Playgroud)

但是如何防止重定向

ged*_*ski 13

现在可以在Angular中取消FYI取消路线.

$scope.$on('$locationChangeStart', function(event, newUrl, oldUrl) {
  event.preventDefault();
});
Run Code Online (Sandbox Code Playgroud)