简体   繁体   English

防止angularjs ui-router路由

[英]Prevent angularjs ui-router from routing

I have a layout for my application that displays the user's name in the top right corner or the page. 我的应用程序布局在右上角或页面显示用户名。 When clicked, a drop down appears with options to navigate to settings, sign out, profile, etc. 单击时,会出现一个下拉列表,其中包含导航到设置,注销,配置文件等的选项。

We got the template from our creative team. 我们从我们的创意团队获得了模板。 They are using an anchor tag () with href=# to prevent navigation. 他们使用带有href =#的锚标签()来阻止导航。 Now that I've incorporated angular and am using ui-router, this approach obviously no longer works. 现在我已经将角度和使用ui-router结合起来了,这种方法显然不再适用。

Is there a way to prevent ui-router from navigating under certain conditions? 有没有办法防止ui-router在某些条件下导航? One obvious answer would be to change the anchor tag to a button. 一个明显的答案是将锚标记更改为按钮。 I am looking for another alternative. 我正在寻找另一种选择。

Or perhaps I need a new approach altogether? 或者我可能需要一种全新的方法?

You could register an ng-click on the anchor, and navigate via $state.go based on your condition inside the handler instead of using ui-sref . 您可以在锚点上注册ng-click,并根据处理程序内的条件通过$state.go导航,而不是使用ui-sref

<a ng-click="clickAction($event)" href="#">Route</a>

And in the click handler prevent the default behavior: 并在单击处理程序中防止默认行为:

  //Make sure to inject `$state`
   $scope.clickAction = function($event){
      $event.preventDefault();

      if(myconditionNotToNavigateMet){
        return;
      }

      $state.go('myUrl');
   }

Is very simple, use the anchor tag without value: 很简单,使用没有值的锚标记:

<a href class="classes">
  ...
</a>

This makes the anchor effect do not miss. 这使得锚效应不会错过。

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

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