使用angular js阻止客户端点击地址栏时的导航

Block navigation when client hits in address bar using angular js

本文关键字:地址栏 导航 端点 客户端 js angular 客户 使用      更新时间:2023-09-26

你好,当用户在地址栏输入/定价时,我不得不停止导航并且只在触发$location.path("/pricing")时导航

when("/pricing", {
        templateUrl: "app/components/Pricing/pricing.html",
    })

如何做到这一点?

您可以做的一件事是设置flag并仅在标志为true时导航,否则防止locationChangeStart中的默认操作。

yourApp.run(['$rootScope', function ($rootScope) {
  $rootScope.$on('$locationChangeStart', function (event, next, current) {
       if($location.path() == '/pricing' && !flag) 
         event.preventDefault(); // This prevents the navigation from happening
      }
    );
  }]);