12

I've setup navigation as follows, using ng-repeat, which works very well

<a ui-sref="{{link.Route}}" ng-click="clickLink(link)">
    <span class="title"> {{link.Text}} </span><span class="selected"></span>
</a>

However, my navigation items frequently have sublinks, which means the parent link isn't really a navigation link, it's just used to expand and view the sublinks. But sometime it is a link, and has no sublinks to display.

The problem is for those particular cases, when there is no state available, I need to remove the ui-sref all together, because there shouldn't be a link at all. Having it there is throwing 'Error: Invalid state ref '''

How do I remove the ui-sref when a state isn't available?

Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299
mrb398
  • 1,277
  • 4
  • 24
  • 32

2 Answers2

16

You could use {{}} with expression

Markup

ui-sref="{{expression ? '.childState' : '.'}}"

. will create own state route, so while click on it, it will redirect no where.

Hope this could help you, Thanks.

Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299
  • What do you mean "will create own state route, so while click on it, it will redirect no where.", I get error on click on the link - "Cannot transition to abstract state '.' " – Pumych Jul 20 '16 at 13:01
  • That's the exception(or you can also call it as rule), by design you can't navigate to abstract state – Pankaj Parkar Jul 20 '16 at 13:06
  • Can you please explain? How to "disable" the link without error? Currently dirty solution that I have is to call function with e.preventDefault() – Pumych Jul 20 '16 at 13:14
  • You can't disable anchor tag, so event.prevenDefault is one of the way to do it.. alternative way you can do is, iinstead of using ui-sref , you could use `ng-click` call a function and redirect from a function using `$state.go` – Pankaj Parkar Jul 20 '16 at 13:34
9

Conditionally create the ui-sref attribute

<a ng-attr-ui-sref="{{ link.Route ? link.Route : false }}">
    ...
</a>
Brett
  • 107
  • 1
  • 5