Is it possible to conditionally display element attributes using Angular2? [duplicate]

不打扰是莪最后的温柔 提交于 2019-12-28 05:27:24

问题


I have some code that uses Polymer attributes on elements, for example:

<paper-drawer-panel force-narrow>

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.

Edit 2 - I understand the rules, but still feel entitled to complain.


回答1:


Simple attribute binding does what you want:

[attr.force-narrow]="someField ? true : null" 

the attribute is not removed when someField is false, only when it's null



来源:https://stackoverflow.com/questions/36240736/is-it-possible-to-conditionally-display-element-attributes-using-angular2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!