【发布时间】:2015-08-11 21:19:32
【问题描述】:
假设有一个简单的 HTML 模板:
<div>
<content-a></content-a>
<content-b></content-b>
</div>
我使用注册两个组件(例如组件“content-a”):
ko.components.register('content-a', {
viewModel: { instance: vm }, // ViewModel for that component
template: template // template of that component
});
然后我将 HTML 模板注入到指定的 HTML 元素中:
var node = document.getElementById('content');
node.innerHTML = template; // here "template" represent just a HTML string (described at the very top)
ko.applyBindings(vm, node); // here vm represents ViewModel instance
但是,当我应用绑定时,所有注册的组件都会被渲染。
有没有办法按需渲染组件?应用 applyBindings 时不会。
换句话说:我想渲染主要内容、applyBindings,然后根据需要添加和渲染新组件。
【问题讨论】:
标签: javascript knockout.js rendering components