我创建了自定义绑定处理程序“foreachprop”
ko.bindingHandlers.foreachprop = {
transformObject: function (obj) {
var properties = [];
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
properties.push({ key: key, value: obj[key] });
}
}
return properties;
},
init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
var value = ko.utils.unwrapObservable(valueAccessor()),
properties = ko.bindingHandlers.foreachprop.transformObject(value);
ko.applyBindingsToNode(element, { foreach: properties });
return { controlsDescendantBindings: true };
}
};
如果我遍历对象
<div data-bind="foreachprop: sections"></div>
这可行,但是如果我使用无容器控制,它不适用于相同的数据
<!-- ko foreachprop: sections --> <!-- /ko -->
如何为自定义绑定处理程序使用无连续控制流
Jsfiddle 创建了新的http://jsfiddle.net/E6xq2/5/