简体   繁体   English

如何使用具有空白ui-sref属性的angular-ui-router?

[英]How can I have angular-ui-router with a blank ui-sref attribute?

I'm writing a dropdown nav in bootstrap and angular with angular-ui-router. 我正在使用angular-ui-router在bootstrap和angular中编写一个下拉导航。 When a link has a dropdown, the link should be blank. 当链接有下拉列表时,链接应为空白。 But if I pass a blank route to ui-sref I get an error. 但是,如果我将一条空白路径传递给ui-sref,我会收到错误消息。

Here's a code snippet: 这是一段代码片段:

<ul class="dropdown-menu">
  <li ng-repeat="button in buttons" id="foo" ng-class="{'dropdown-submenu': $index=button.hasLinks}" >
    <a ui-sref="{{button.route}}" title="{{ button.text }} Tab" class="{ dropdown-toggle: button.links }" data-toggle="dropdown">
      <i class="icon icon-{{ button.icon }}"></i> {{ button.text }}
    </a>
    <ul class="dropdown-menu">
      <li ng-repeat="link in button.links">
        <a ng-href="{{ link.route }}" title="{{ link.text }} Tab">
          {{ link.text }}
        </a>
      </li>
    </ul>
  </li>
</ul>

Currently, as you can read in this topic , ui-sref doesn't handle empty argument, as it is (quoting the author) a misuse of the directive. 目前,正如您可以阅读本主题所述ui-sref不处理空参数,因为它(引用作者)滥用指令。 Upcomming ui-state directive is supposed to handle that, but for now the best way I found was (in Angular 1.3 and above) to use ng-attr as below: upcomming ui-state指令应该处理它,但是现在我发现的最好的方法是(在Angular 1.3及以上版本中)使用ng-attr如下:

ng-attr-ui-sref="{{ state || undefined }}"

If state is blank, it will yield undefined, and no attribute in effect. 如果state为空,则它将产生undefined,并且没有有效的属性。

EDIT: Note that as long as it works, it will throw an exception in the console due to ui-router open bug 编辑:请注意,只要它工作,它将在控制台中因ui-router open bug而引发异常

I solved this issue by not using ui-sref, but rather ng-href: 我通过不使用ui-sref解决了这个问题,而是使用了ng-href:

<a ng-href="{{ ::someObject.href }}">some link</a>

controller : 控制器

someObject.href = $state.href('FancyState', params: { id: 123 });

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

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