gpt4 book ai didi

javascript - 覆盖 knockout 绑定(bind) : cannot apply bindings multiple times error

转载 作者:行者123 更新时间:2023-12-03 05:10:13 27 4
gpt4 key购买 nike

knockout中,我想覆盖默认的foreach绑定(bind):

var foreachInit = ko.bindingHandlers.foreach.init;
ko.bindingHandlers.foreach.init = function () {
foreachInit.apply(this, arguments);
};

但即使这个简单的代码片段也会触发错误:

You cannot apply bindings multiple times to the same element

请参阅 fiddle 控制台:https://jsfiddle.net/hejdav/wxf51s5L/10/

你知道为什么会发生这种情况吗?

最佳答案

我相信你在这里想得太多了。创建一个 viewModel 构造函数,初始化并传递所需的数据,然后将其存储在 observableArray 中。

var ViewModel = function(r) {
// if r exists place it in the observable otherwise it's an empty array
this.items = ko.observableArray(r || []);
};

var model = new ViewModel([1, 2]);
ko.applyBindings(model);
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<p data-bind="foreach: items">
<span data-bind="text: $data"></span>
</p>

关于javascript - 覆盖 knockout 绑定(bind) : cannot apply bindings multiple times error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41854875/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com