問題描述
我有一些代碼在元素上使用 Polymer 屬性,例如:
I have some code that uses Polymer attributes on elements, for example:
<paper-drawer-panel force-narrow>
聚合物的工作方式是,如果我想禁用該屬性,我不能簡單地說 force-narrow="false"
,它必須完全刪除.Angular2中有沒有辦法有條件地刪除整個屬性?我似乎找不到有關此主題的任何信息.我可以使用 ngIf
并在有和沒有屬性的情況下重復整個元素,但如果可能的話我寧愿不這樣做.謝謝!
The way polymer works is that I can't simply say force-narrow="false"
if I want to disable the attribute, it has to be removed altogether. Is there a way in Angular2 to conditionally remove the entire attribute? I can't seem to find any information on this topic. I could use ngIf
and repeat the entire element with and without the attribute but I would rather not if possible. Thanks!
編輯 - 為什么這個問題被標記為重復并關閉?我在有問題的重復帖子之前問了這個問題.天哪,這個網站有時很糟糕.
Edit - Why was this question marked and closed as a duplicate? I asked this question a month before the duplicate post in question. God this site sucks sometimes.
編輯 2 - 我了解規則,但仍然覺得有權投訴.
Edit 2 - I understand the rules, but still feel entitled to complain.
推薦答案
簡單的屬性綁定做你想做的事:
Simple attribute binding does what you want:
[attr.force-narrow]="someField ? true : null"
當 someField
為 false
時該屬性不會被移除,只有當它為 null
the attribute is not removed when someField
is false
, only when it's null
這篇關于是否可以使用 Angular2 有條件地顯示元素屬性?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!