0

I would like to display the following HTML with angular value; ONLY IF the value exists, how could I wrap the below around an if statement, to check if a value exists FIRST, then if it does; display this inner html.

I'd like to do this inline; ideally with a ternary operator.

<span class="bloCK" ng-mouseover="showPopover()" ng-mouseleave="hidePopover()"><a href="{{website.coolUrl}}" target="_blank">Cool Website</a></span>
3
  • 2
    What version of Angular are you using? Because this looks more like AngularJS, which is quite different from Angular... Commented Sep 12, 2019 at 15:05
  • AngularJS you are correct; sorry for my typo! Commented Sep 12, 2019 at 15:08
  • Possible duplicate of How can I conditionally display HTML in a button using AngularJS? Commented Sep 12, 2019 at 15:11

1 Answer 1

1

I'm guessing that "the value" you're talking about is website.coolUrl, in which case you can do:

<span ng-if="website && website.coolUrl"
    class="bloCK" ng-mouseover="showPopover()"
    ng-mouseleave="hidePopover()">
  <a href="{{website.coolUrl}}" target="_blank">Cool Website</a></span>

Update... Oops!

It's only after I posted this that I saw you meant AngularJS, not Angular. I modified my answer for that, but it's been a long time since I did any AngularJS.

Sign up to request clarification or add additional context in comments.

3 Comments

ng-if="website && website.coolUrl" makes no sense, simple ng-if="website.coolUrl" does same. To avoid problems one always prefers ng-href to href
Since I didn't have all of the particulars of the situation, I couldn't know for sure if website itself could always be trusted to be defined. In that case, website && website.coolUrl is safer, as it prevents trying to access a property of a potentially undefined value. I'm not sure about AngularJS, but Angular also supports the form website?.coolUrl for safe navigation.
I understand that from Angular point of view it may seem "safer" :) But in AngularJS world template never complains that something is undefined. And it saves you tons of code... e.g. "a && a.b && a.b.c && a.b.c.d"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.