How to conditionally render a css class with knock

2019-03-17 13:09发布

问题:

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



标签: knockout.js