admin管理员组文章数量:1389652
I currently have two different working implementations of [ngClass]
on an element;
[ngClass]="{ selected: element.isSelected, highlighted: element.isHighlighted}"
and
[ngClass]="element.customClasses"
Is it possible to bine both of these approaches in the template, or do I have to create a method in my ponent to return an array of classes based on the logic above?
Thanks!
I currently have two different working implementations of [ngClass]
on an element;
[ngClass]="{ selected: element.isSelected, highlighted: element.isHighlighted}"
and
[ngClass]="element.customClasses"
Is it possible to bine both of these approaches in the template, or do I have to create a method in my ponent to return an array of classes based on the logic above?
Thanks!
Share Improve this question asked Feb 12, 2017 at 18:28 Chris BrownChris Brown 4,6553 gold badges31 silver badges37 bronze badges2 Answers
Reset to default 8I opted for using [class.*]
to set the conditional classes, leaving [ngClass]
to handle the binding;
<div
[ngClass]="element.customClasses"
[class.selected]="element.isSelected"
[class.highlighted]="element.isHighlighted"
></div>
You can directly set the customClasses to your template as
<span class="customClasses">something</span>
And also you can use [ngClass] which will append your classes based on the condition, so putting together
<span [ngClass]="{ selected: element.isSelected,
highlighted: element.isHighlighted}"
class="customClasses">something
</span>
本文标签: javascriptAngular2 ngClassCombining direct binding with conditional classesStack Overflow
版权声明:本文标题:javascript - Angular2 [ngClass] - Combining direct binding with conditional classes - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744618956a2615905.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论