這項診斷會確保:帶有 Angular 繫結專用前綴(attr.、style. 與 class.)的屬性,能被正確解讀為繫結。
<div attr.id="my-id"></div>
這樣寫有什麼問題?
在這個範例中,attr.id 會被解讀為一般屬性,並原封不動地出現在最終的 HTML 中(<div attr.id="my-id"></div>)。這很可能不是開發者的本意。
開發者的本意很可能是要設定 id 屬性(<div id="my-id"></div>)。
我應該怎麼做?
繫結到 attr.、class. 或 style. 時,請確認使用 Angular 範本繫結語法([])。
<div [attr.id]="'my-id'"></div>
<div [style.color]="'red'"></div>
<div [class.large]="true"></div>
設定需求
任何擴充診斷要發出訊息,都必須啟用 strictTemplates。
textAttributeNotBinding 除了 strictTemplates 之外,沒有其他額外需求。
如果無法避免,該怎麼辦?
可以透過編輯專案的 tsconfig.json 檔案來停用這項診斷:
{
"angularCompilerOptions": {
"extendedDiagnostics": {
"checks": {
"textAttributeNotBinding": "suppress"
}
}
}
}
詳情請見擴充診斷設定。