微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐