knockout.js – 敲除css绑定渲染=> class =“0 1 2 3 4 5 6 with KO 2.0

前端之家收集整理的这篇文章主要介绍了knockout.js – 敲除css绑定渲染=> class =“0 1 2 3 4 5 6 with KO 2.0前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我用这段代码得到一个奇怪的样式文本:
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

Knockout 2.2.0 released开始:

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,它会正常工作.

如果无法升级,可以使用attr绑定作为解决方法

<button data-bind="text: followButtonText,attr:{ class: styleClass}" />

演示JSFiddle using KO 2.0.0 and attr binding

原文链接:https://www.f2er.com/js/158535.html

猜你在找的JavaScript相关文章