Knockout.js v2.3.0 错误“您不能对同一个元素多次应用绑定" [英] Knockout.js v2.3.0 error "You cannot apply bindings multiple times to the same element"

查看:18
本文介绍了Knockout.js v2.3.0 错误“您不能对同一个元素多次应用绑定"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚升级到 Knockout.js 2.3.0,现在出现此错误:

不能对同一个元素多次应用绑定.

我在 2.2.1 中没有得到.

我正在从我的 MVC 控制器获取部分视图,并在单击 href 后将其添加到页面.第二次单击链接以获取局部视图时发生错误.我多次这样做.

有没有办法清除它并避免抛出新错误?

这是我的代码:

$.get(url + "GetAssignedCompaniesView?layoutId=" + layoutId + "&noCache=" + new Date().getMilliseconds(),函数(结果){$("#editAssignedPartial").html($(result));showEditAssignedArea(true);$(window.document).ready(function () {//这里抛出错误ko.applyBindings(self, window.document.getElementById(editAssigned"));$("#layoutId").attr("value", layoutId);updateTypeHiddenElement.attr(价值",公司");});});

这是我的 HTML:

<!-- 此处带有链接的网格-->

<div id="editAssignedPartial"></div>

$(document).ready(function () {'使用严格';var vm = 新的 Vm();ko.applyBindings(vm, document.getElementById(area1"));});

解决方案

在再次使用 applyBindings 之前,您需要移除绑定:

ko.cleanNode($element[0]);

I've just upgraded to Knockout.js 2.3.0 and now I'm getting this error:

You cannot apply bindings multiple times to the same element.

which I wasn't getting in 2.2.1.

I'm getting a partial view from my MVC controller and adding it to the page after clicking on an href. The error happens the second time I click on the link to get the partial view. I'm doing this multiple times.

Is there a way to clear this out and avoid the new error thrown?

Here's my code:

$.get(url + "GetAssignedCompaniesView?layoutId=" + layoutId + "&noCache=" + new Date().getMilliseconds(), function (result) {
    $("#editAssignedPartial").html($(result));
    showEditAssignedArea(true);
    $(window.document).ready(function () {
        // error is thrown here
        ko.applyBindings(self, window.document.getElementById("editAssigned"));
        $("#layoutId").attr("value", layoutId);
        updateTypeHiddenElement.attr("value", "companies");
    });
});

Here's my HTML:

<div id="area1">
    <!-- grid here with links -->
</div>
<div id="editAssignedPartial"></div>

$(document).ready(function () {
    'use strict';
    var vm = new Vm();
    ko.applyBindings(vm, document.getElementById("area1"));
});

解决方案

You need to remove the bindings before you use applyBindings again:

ko.cleanNode($element[0]);

这篇关于Knockout.js v2.3.0 错误“您不能对同一个元素多次应用绑定"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆