无法在cleanNode之后应用绑定

时间:2015-11-10 19:36:28

标签: knockout.js

我有下面的代码,当我第一次在' msg'中有一些价值时,这很好。但是,当脚本第二次运行时,如果没有' msg',它也没关系。问题是我再次运行它的第三次,它没有绑定值,它没有在html中渲染我的span。这是代码。请让我知道我失踪了什么。

    var msg = '';
if (splitArray._latestValue != null) {
msg = splitArray._latestValue[1].concat(" was merged to ",splitArray._latestValue[2], ".");
}
var loyaltyIdModel = {
    loyaltyMessage: ko.observable(msg)
}
ko.applyBindings(loyaltyIdModel, document.getElementById("loyaltyId"));
ko.cleanNode(document.getElementById("loyaltyId"));

我终于通过这种方式来解决这个问题。

var msg = 'notmerged';
if (splitArray._latestValue != null) {
    msg = splitArray._latestValue[1].concat(" was merged to ", splitArray._latestValue[2], ".");
}
var loyaltyIdModel = {
    loyaltyMessage: ko.observable('notmerged')
}

loyaltyIdModel.loyaltyMessage(msg);

ko.applyBindings(loyaltyIdModel, document.getElementById("loyaltyId"));
ko.cleanNode(document.getElementById("loyaltyId"));


<div id="loyaltyId" class="alert-info" data-bind="if: loyaltyMessage() !== 'notmerged'">
    <span data-bind="text: loyaltyMessage"> </span>
</div>

这是代码的最新更新。我没有使用cleanNode就能做到这一点。感谢。

var viewModel = ko.dataFor(getSearchPanelNode());
var tempModel = ko.dataFor(document.getElementById("loyaltyId"))

if (viewModel) {
    viewModel.result.setItems(model.result.items);
    tempModel.loyaltyMessage(msg);
}
else {
    applyViewModel(model);
    var loyaltyIdModel = {
        loyaltyMessage: ko.observable()
    };
    loyaltyIdModel.loyaltyMessage(msg);
    ko.applyBindings(loyaltyIdModel, document.getElementById("loyaltyId"));
}

0 个答案:

没有答案