How to conditionally render a css class with knockoutjs

家住魔仙堡 提交于 2020-01-01 01:16:31

问题


I have some html like the following:

<div class="control-group">
    <input type="text" data-bind="value: $data.DealCode" name="DealCode" class="input-mini" />
</div>

However, ifnot: $data.DealCodeIsValid, I need to render the following:

<div class="control-group error">
    <input type="text" data-bind="value: $data.DealCode" name="DealCode" class="input-mini" />
</div>

Note the additional class "error" in the div. Is there a way to do that with knockoutjs?


回答1:


Something like

<div data-bind="css: {'control-group': true, error: (!$data.DealCodeIsValid)}">

Check here for more info



来源:https://stackoverflow.com/questions/11014336/how-to-conditionally-render-a-css-class-with-knockoutjs

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