Knockout JS - Use both static class name as well as data bound class name

前端 未结 3 1185
广开言路
广开言路 2021-02-05 23:17

Anyone know if I can have an html element with both a static class name as well as a dynamic, data-bound class name using KnockoutJS? Something like this:



        
3条回答
  •  北海茫月
    2021-02-05 23:25

    You can use the css binding to add and remove a class based on the truthiness of a value, but it sounds like your viewModelProperty is the name of a class rather than a boolean.

    You can use the attr binding with the static class included like: (attr: { 'class': 'staticClassName ' + viewModelPropertyValue } or (viewModelPropertyValue() if it is an observable).

    Otherwise, there is a community binding here that will add and remove a class in the manner that you are after: https://github.com/SteveSanderson/knockout/wiki/Bindings---class

提交回复
热议问题