Knockout.js:更改Observable的值不会更改html中attribute的值

Knockout.js: Changing the value of Observable does not change the value of attribute in html

本文关键字:html 的值 attribute js 更改 Observable Knockout      更新时间:2023-09-26

我有一个相当简单的文件,它可以呈现这样的铅笔图像:

define(['durandal/events', 'knockout'], function (events, ko) {
    var pencilVM= function () {
    this.pencilimagepath = ko.observable("img/pencil.png");
    };
    return pencilVM;
});

和一个html:

<div>
 <img data-bind="attr: {src: pencilimagepath}" />
</div>

现在,当我将图像的名称从pencil.png更改为someImg.png时,浏览器中呈现的最终html不会更新。它仍然保持为"pencil.png"

我认为浏览器正在缓存图像。

只需将时间戳添加到您的新图像源

var d = new Date();
this.pencilimagepath("img/someImg.png" + d.getTime());

希望能有所帮助。