什么时候使用 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屋!