何时使用ng-attr? [英] When to use ng-attr?
问题描述
似乎使用直接属性和ng-attr-*
指令可以做相同的事情.例如,以下等价渲染:
It seems like using direct attributes and the ng-attr-*
directive do the same thing. For example, the following render equivalently:
<div ng-attr-id="{{ 'object-' + value }}">ID</div>
<div id="{{ 'object-' + value }}">ID</div>
何时应使用ng-attr-*
,何时应使用直接HTML属性?
When should I use ng-attr-*
and when should I use the direct HTML attribute?
推荐答案
ng-attr用于在上下文中添加或不添加属性.如果表达式为{{undefined || null}}
,则不添加属性,否则,如果属性具有值,则将属性添加为值{{ value }}
.
最常见的情况是插值.
有关的:
在Angular指令模板中有条件地添加数据属性
ng-attr is used for add or not the attribute in context. If the expression {{undefined || null}}
, the attribute is not added otherwise if has a value then attribute is added with the value {{ value }}
.
The most common cases is in interpolation.
Related:
Conditionally adding data-attribute in Angular directive template
这篇关于何时使用ng-attr?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!