同
with
繫結將繫結節點內的 HTML 繫結到單獨的上下文:
<div data-bind="with: subViewModel">
<p data-bind="text: title"></p>
</div>
也可以在沒有容器元件的情況下使用 with
繫結,其中容器元件可能不合適。
<!-- ko with: subViewModel -->
<p data-bind="text: title"></p>
<!-- /ko -->
var vm = {
subViewModel: ko.observable()
};
// Doesn't throw an error on the `text: title`; the `<p>` element
// isn't bound to any context (and even removed from the DOM)
ko.applyBindings(vm);
// Includes the `<p>` element and binds it to our new object
vm.subViewModel({ title: "SubViewModel" });
with
結合與 template
或 foreach
結合有許多相似之處。