首页 文章

Ui-router:如何阻止网址导航但接受状态导航

提问于
浏览
0

我尝试做一些接受状态导航的功能(使用$ state.go(otherState)),刷新地址/网址栏中的关联网址,但是如果用户直接将此网址放入,则会阻止(或重定向到不允许的网页)地址/网址栏 .

可以通过ui-router规则或ui-router模块内的东西来完成吗?

我把示例代码:

$stateProvider.state("main", {
  url: "/index.html",
  templateUrl: "main.html"
}).state("notAccessibleScreenByBar", {
  url: "/private/example.html",
  templateUrl: "example.html"
});

从主视图(index.html)开始,将执行下一个角度代码:

$state.go("notAccessibleScreenByBar");

此操作会更改视图,加载example.html并将网址栏刷新为/private/example.html .

如果用户将/private/example.html放入地址/网址栏,则ui-router必须阻止此请求(或重定向到不允许的网页) .

1 回答

  • 0

    您尝试做的似乎与任何Web身份验证标准非常相似,但是,如果您不希望这样,您可以使用 $locationChangeSuccessdocs here

    this sample启发的基本示例:

    $rootScope.$on('$locationChangeSuccess', function(e, newUrl, oldUrl) {
    
        // Prevent $urlRouter's default handler from firing
        e.preventDefault();
    
        if(isThisTransitionValid(newUrl, oldUrl)) {
          // Ok, let's go
          $urlRouter.sync();
        });
      });
    

相关问题