本文介绍了我们应该在调用ko.applyBindings之前等待DOM加载吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照标题,是否有必要在调用ko.applyBindings之前等待DOM加载,还是Knockout会自动处理此问题?

As per the title, is it necessary to wait for the DOM to load before calling ko.applyBindings or will Knockout handle this automatically?

即-我可以安全地这样做吗?

I.e - am I safe to just do:

<script>
(function() {

    var model = new my.Model();
    ko.applyBindings(model);

})();
</script>

推荐答案

没有KO无法自动处理此问题(因此,自调用功能仅在页面底部起作用),您必须等待DOM加载完毕ko.applyBindings调用.

No KO doesn't handle this automatically (so the self invoking function would work only at the bottom your page), you have to wait for the DOM loaded with the ko.applyBindings call.

文档:

ko.applyBindings(myViewModel);

您可以将脚本块放在 HTML文档的底部,也可以将其放在顶部,然后 将内容包装在支持DOM的处理程序中,例如jQuery的$函数.

You can either put the script block at the bottom of your HTML document, or you can put it at the top and wrap the contents in a DOM-ready handler such as jQuery’s $ function.

这篇关于我们应该在调用ko.applyBindings之前等待DOM加载吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 03:37