根据标题,在调用ko.applyBindings
之前是否需要等待DOM加载?或者 Knockout 会自动处理吗?
即 - 我可以安全地这样做:
<script>
(function() {
var model = new my.Model();
ko.applyBindings(model);
})();
</script>
最佳答案
没有 KO 不会自动处理这个(所以自调用函数只能在页面底部工作),你必须等待加载了 ko.applyBindings
的 DOM。称呼。
来自 documentation :
To activate Knockout, add the following line to a
<script>
block:
ko.applyBindings(myViewModel);
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.
关于knockout.js - 我们是否应该在调用 ko.applyBindings 之前等待 DOM 加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14737212/