<div id="metro-scrollbar" data-bind="foreach: data.sections , visible: data.sections.length > 0">
<section data-bind="foreach: $data.tiles, visible: $data.tiles.length > 0, css: { 'last' : $parent.isLastSection($index)}">
<div data-bind="attr : {'class' : $root.getClass($data,$parentContext.$index,$index)} ">
<div data-bind="attr : {'class' : $root.getAspectClass($data,$index)} "></div>
<div class="live-tile">
<span class="tile-title" data-bind="text: title, css:{'big' : bigtitle}"></span>
<div>
<p>a</p>
</div>
</div>
</div>
</section>
</div>
我有上述绑定。我需要在一些casses中为内部div注入一个包装器。
<div>
<section>
<-- if $myFunction() inject <div> -->
<div>
<div></div> // THIS IN HERE SHOULD BE THERE ALWAYS:
<div > // THIS IN HERE SHOULD BE THERE ALWAYS:
</div> // THIS IN HERE SHOULD BE THERE ALWAYS:
</div>
<-- if $myFunction() inject </div> -->
</section>
</div>
我希望这是有道理的。
答案 0 :(得分:3)
s093294有正确的想法。以下是解决方案的示例(以及jsFiddle):
查看强>
<button data-bind="click: click">Toggle</button>
<section data-bind="template: { name: templateName }"></section>
<script type="text/html" id="outer-tmpl">
<div data-bind="template: { name: 'inner-tmpl' }">
</div>
</script>
<script type="text/html" id="inner-tmpl">
<div>first inner div</div>
<div>second inner div</div>
</script>
<强>模型强>
viewModel = {
toggle: new ko.observable(true),
templateName: function () {
return viewModel.toggle() ? 'outer-tmpl' : 'inner-tmpl';
},
click: function () {
viewModel.toggle(!viewModel.toggle());
}
}
ko.applyBindings(viewModel);