我想将我的 knockout View 模型定位到 dom 的特定部分,如下所示:
ko.applyBindings(MyViewModel,$('#Target')[0]);
但是我不希望它应用于它下面的所有 dom。这样做的原因是整个 SPA 工作不是很好——无法跟上由于将每个潜在交互包含到一个巨大对象中而产生的巨型 View 模型。因此,该页面由多个部分 View 组成。我希望每个部分实例化自己的 ViewModel 并为父级提供交互界面。
一些样本dom
<div id="Target">
<!--Everything here should be included except-->
<div data-bind="DoNotBindBelowThis:true">
<!--Everything here should NOT be included by the first binding,
I will specifically fill in the binding with targetted
ApplyBind eg. ko.applyBindings(MyOtherViewModel, $('#MyOtherTarget')[0])
to fill the gaps-->
<div id="MyOtherTarget">
</div>
</div>
</div>
同样,如何从 applyBindings
中排除带有 DoNotBindBelowThis
标记的 div 下面的整个 dom 树?
最佳答案
查看此处的博文:http://www.knockmeout.net/2012/05/quick-tip-skip-binding.html
基本上,您可以像这样创建一个自定义绑定(bind):
ko.bindingHandlers.DoNotBindBelowThis = {
init: function() {
return { controlsDescendantBindings: true };
}
};
关于javascript - 从 knockout 应用绑定(bind)中排除 DOM 元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15395872/