我们是否应该在调用 ko.applyBindings 之前等待 DOM 加载 [英] Should we wait for DOM to load before calling ko.applyBindings

查看:14
本文介绍了我们是否应该在调用 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.

来自文档:

要激活 Knockout,请将以下行添加到

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