这在此处的文档中进行了描述:http: //knockoutjs.com/documentation/custom-bindings-controlling-descendant-bindings.html
来自文档:
with 和 foreach 等绑定在绑定上下文层次结构中创建了额外的级别。这意味着它们的后代可以使用 $parent、$parents、$root 或 $parentContext 访问外部级别的数据。如果您想在自定义绑定中执行此操作,则不要使用 bindingContext.extend(),而是使用 bindingContext.createChildContext(someData)。
例子:
ko.bindingHandlers.withProperties = {
init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
// Make a modified binding context, with a extra properties, and apply it to descendant elements
var newProperties = valueAccessor(),
childBindingContext = bindingContext.createChildContext(viewModel);
ko.utils.extend(childBindingContext, newProperties);
ko.applyBindingsToDescendants(childBindingContext, element);
// Also tell KO *not* to bind the descendants itself, otherwise they will be bound twice
return { controlsDescendantBindings: true };
}
};