Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
189
rated 0 times [  190] [ 1]  / answers: 1 / hits: 27318  / 8 Years ago, wed, march 30, 2016, 12:00:00

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



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>



Can I avoid this with something like a ternary operator ? for example ...



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

More From » angularjs

 Answers
265

Why do you ask if you already know the answer :-)? Try it out, it is indeed



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

[#62758] Monday, March 28, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
arthur

Total Points: 729
Total Questions: 107
Total Answers: 109

Location: China
Member since Mon, Aug 22, 2022
2 Years ago
;