Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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.

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>
like image 347
Grant Eagon Avatar asked Mar 20 '14 20:03

Grant Eagon


People also ask

What is ui-sref in angular?

ui-sref stands for UI-Router state reference. It's a way to change states/state params (as defined in using the $stateProvider in a config block using the ui. router module for AngularJS.

What is ui-sref active in AngularJS?

ui-sref-active can live on the same element as ui-sref / ui-state , or it can be on a parent element. If a ui-sref-active is a parent to more than one ui-sref / ui-state , it will apply the CSS class when any of the links are active.

What is sref HTML?

A ui-sref is a directive, and behaves similar to an html href . Instead of referencing a url like an href , it references a state. The ui-sref directive automatically builds a href attribute for you ( <a href=...> </a> ) based on your state's url.

What is $state in AngularJS?

$stateProvider is used to define different states of one route. You can give the state a name, different controller, different view without having to use a direct href to a route. There are different methods that use the concept of $stateprovider in AngularJS.


Video Answer


1 Answers

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. 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:

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

If state is blank, it will yield undefined, and no attribute in effect.

EDIT: Note that as long as it works, it will throw an exception in the console due to ui-router open bug

like image 165
wap300 Avatar answered Sep 27 '22 17:09

wap300