问题
I have a page that requires jQuery.tmpl, but I want to use native knockout templating for a
data-bind="foreach: Comments"
attribute. Because I have included jQuery.tmpl, knockout's native templating is disabled; is there a way that I can force the native functionality?
Thanks
回答1:
You cannot use foreach
or other control-flow bindings within a jQuery.tmpl template.
However, if you want to call a named template and force it to use the native template engine, then you would do something like:
<div data-bind="template: { name: 'itemsTmpl', templateEngine: new ko.nativeTemplateEngine() }">
</div>
<script id="itemsTmpl" type="text/html">
<ul data-bind="foreach: items">
<li data-bind="text: $data"></li>
</ul>
</script>
or cache a copy of a native template engine (new ko.nativeTemplateEngine()
) in a variable.
来源:https://stackoverflow.com/questions/9750724/force-native-knockout-templating