Knockout template using data-bind to image src property not working

老子叫甜甜 提交于 2019-11-28 18:09:26

问题


I cannot see what is wrong here but the image does not display using the following Knockout template:

<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>

The object this is being bound to looks like this:

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?");
     }
};

When the HTML object is rendered I see the label and clicking on the checkbox invokes doSomething.

TIA.


回答1:


Only a few attributes can be bound directly; try using attr - it will let you set any attribute on an element.

<img width="16px" height="16px" data-bind="attr:{src: imagePath}" />  


来源:https://stackoverflow.com/questions/10659665/knockout-template-using-data-bind-to-image-src-property-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!