我用这段代码得到一个奇怪的样式文本:
self.styleClass = ko.computed(function () { return self.isFollowing() ? "button" : "secondary button"; }); <button data-bind="text: followButtonText,click: toggleIsFollowing,css: styleClass"></button>
它呈现:
<button data-bind="text: followButtonText,css: styleClass" class=" 0 1 2 3 4 5 6 7 8 9 10 11 12">Unfollow</button>
model.styleClass()在控制台中渲染得很好
解决方法
css绑定的语义已从KO 2.1.0更改为KO 2.2.0
We’ve also made some features work more like you might always have
thought they should work. For example,the css binding can Now attach
programmatically-generated CSS class names to elements (prevIoUsly,it
was limited to toggling predefined CSS class names).
所以升级到KO 2.2.1,它会正常工作.
<button data-bind="text: followButtonText,attr:{ class: styleClass}" />