Error adding ternary condition within a ui-sref

Asked

Viewed 33 times

0

I’m making a link on a page that will redirect according to the object data. So I made a ternary condition, but it’s giving a syntax error and I can’t see what’s wrong:

My html:

<a class="article-home-small" ui-sref="obj.content_type == 'article' ? article({uid: obj.uid, title: slug(obj.title)}) : article_blog({uid: obj.uid, title: slug(obj.title)})">

The mistake:

Syntax Error: Token ')' is an unexpected token at column 39 of the expression [{uid: obj.uid, title: slug(obj.title)}) : article_blog({uid: obj.uid, title: slug(obj.title)}] starting at [) : article_blog({uid: obj.uid, title: slug(obj.title)}].

1 answer

1

To do it inside ui-sref you need two sets of keys {{}}:

<a class="article-home-small" ui-sref="{{obj.content_type == 'article' ? article({uid: obj.uid, title: slug(obj.title)}) : article_blog({uid: obj.uid, title: slug(obj.title)})}}">
  • Made this mistake: Error: Invalid state ref '' without even me clicking on anything.

  • Try using a function instead ui-sref and puts in the ng-click. The function receives obj as a parameter and then make the logic you need before sending to the right page.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.