How to conditionally set the id attribute of an HTML element using angular js?

I want to set the id attribute of an HTML element conditionally.

A simple way:

<!--  expression = true -->
<div ng-if="expression"> 
  <a href id="this-one">Anchor with id</a>
</div>
<div ng-if="!expression">
  <a href>Anchor without id</a>
</div>
output-if-expression-true = <a href id="this-one">Anchor with id</a>

output-if-expression-false= <a href>Anchor without id</a>

Is it possible to avoid this with the help of a three-dimensional operator? eg...

<a href ng-attr-id="{{expresion ? 'this-one': ''}}">Anchor</a>
+4
source share
1 answer

Why are you asking if you already know the answer :-)? Really try

<a href ng-attr-id="{{expresion ? 'this-one': ''}}">Anchor</a>
+6
source

All Articles