我看不出这里有什么问题,但使用以下 Knockout 模板无法显示图像:
<script type="text/html" id="legend-template">
<div><input type="checkbox" data-bind="click : doSomething" ></input>
<img width="16px" height="16px" data-bind="src: 'imagePath'" />
<span data-bind="text : label"> </span>
</div>
</script>
绑定(bind)到的对象如下所示:
tut.myObject= function (imagePath, label) {
this.label = ko.observable(label);
this.imagePath = ko.observable(imagePath || liveString + '/Content/images/marker.png');
};
tut.myObject.prototype = {
doSomething: function () { alert("do what?");
}
};
当呈现 HTML 对象时,我会看到标签并单击复选框会调用 doSomething。
TIA。
最佳答案
只有少数属性可以直接绑定(bind);尝试使用 attr
- 它可以让您在元素上设置任何属性。
<img width="16px" height="16px" data-bind="attr:{src: imagePath}" />
关于data-binding - 使用数据绑定(bind)到图像 src 属性的 knockout 模板不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10659665/