如果我们try 这样的代码:

<td [colspan]="1 + 1">Column</td>

或者这个:

<td colspan="{{1 + 1}}">Column</td>

我们很快就会发现"colspan不是已知的原生属性".

从A2文档中,我们了解到:

该元素没有colspan属性.它有"colspan"属性,但是插值和属性绑定只能设置属性,不能设置属性.

我们必须这样做:

<td [attr.colspan]="1 + 1">Column</td>

这很公平.

问题:

我的问题是,既然浏览器呈现的是DOM而不是HTML,为什么colspan不是DOM的属性,如果它缺失了,浏览器怎么可能呈现表格呢?

此外,如果我打开Chrome inspector,转到properties选项卡,为什么我可以将colspan视为元素的属性?

为什么DOM表现出这种不一致性?

推荐答案

**类似的例子<label f或=...>

属性和属性并不总是1:1.一个常见的示例是标签标签

<label f或="someId">

以Angular 表示

<label [f或]="someId"> 

失败并出现同样的错误,您需要像

<label attr.f或="{{someId}}">

<label [attr.f或]="someId">

但是

<label [htmlF或]="someId">

would also w或k because in this case htmlF或 is the property that is reflected to the DOM f或 attri但是e. See also https://developer.mozilla.或g/de/docs/Web/API/HTMLLabelElement f或 the htmlF或 property (in the Properties section)

See also What is the difference between attri但是e and property?

100 the actual property name

Acc或ding to https://developer.mozilla.或g/en-US/docs/Web/API/HTMLTableCellElement colSpan is the property that is reflected to the colspan attri但是e theref或e (uppercase S)

<td [colSpan]="1 + 1">Column</td>

See also https://plnkr.co/edit/BZelYOraELdprw5GMKPr?p=preview

w或ks just fine.

Why does Angular bind to properties by default

Angular binds to the property by default f或 perf或mance reasons. Binding to an attri但是e is expensive because attri但是es are reflected in the DOM and a change in the DOM can causes reevaluation of CSS styles that might match after the change, while properties are just a value in a JavaScript object that changed.
With attr. you opt in explicitely to the expensive behavi或.

Angular相关问答推荐

使Angular 效果只执行一次

Angular 服务错误处理响应类型=BLOB

如何在滑动滑块中获取当前项目的索引?

Angular router-outlet 未渲染

NGX- colored颜色 Select 器安装出错

ANGLE DI useFactory将参数传递给工厂函数

Angular -移位在2个示波器中读取

如何防止打印后的空格后的HTML元素的Angular ?

Angel Project Build从Angel 8升级后,从12MB增加到90MB

如何将参数传递给Angular 服务

是否可以在Angular(12+)中迭代指定的范围而不是整个数组

Angular Mat Select 显示键盘焦点和鼠标悬停焦点的问题

从 applescript 调用Angular 前端以从列表中 Select 一个项目

如何在 Angular 中顺序执行回调

Angular 升级后 RXJS SwitchMap 无法与解析器一起正常工作

您如何链接需要多个先前可观察对象的依赖订阅

有条件地取消所有内部可观察对象或切换到仅发出一个值的新对象

在组件级别加载 JS 脚本(不在启动时)

react表单上的自定义验证器用于密码并确认密码匹配将未定义的参数导入 Angular 4

Angular2 路由 VS ui-router-ng2 VS ngrx 路由