簡體   English   中英

如何使用角度js條件設置HTML元素的id屬性?

[英]How to conditional set the id attribute of a HTML element with angular js?

我想有條件地設置HTML元素的id屬性。

簡單方法:

<!--  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> 

我可以用三元運算符之類的東西來避免這種情況嗎? 例如 ...

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

為什么你問你是否已經知道答案:-)? 嘗試一下,確實如此

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

上述解決方案對我不起作用。 以下做了:

id="{{expression ? 'this-one': 'that-one'}}"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM