que*_*326 7 javascript angularjs
我想有条件地设置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>
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)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>
Run Code Online (Sandbox Code Playgroud)
fik*_*tra 10
为什么你问你是否已经知道答案:-)?尝试一下,确实如此
<a href ng-attr-id="{{expresion ? 'this-one': ''}}">Anchor</a>
Run Code Online (Sandbox Code Playgroud)
上述解决方案对我不起作用。做了以下事情:
id="{{expression ? 'this-one': 'that-one'}}"
Run Code Online (Sandbox Code Playgroud)