Cat*_*lin 6 jquery knockout-2.0 knockout.js
我有一个淘汰模板:
<script id="draggableHelper" type="text/x-jquery-tmpl">
<div class="draggableHelper">
<span data-bind="text: Name"></span>
</div>
</script>
Run Code Online (Sandbox Code Playgroud)
是否可以通过发送对象来填充模板来生成模板的结果并将其保存到内存中?
就像是:
var result = ko.renderTemplate($("#draggableHelper").html(), { Name: "Test" });
Run Code Online (Sandbox Code Playgroud)
And*_*ins 14
是的,可以将绑定应用于未附加到DOM的节点.只需使用非常有用的功能ko.applyBindingsToNode即可达到预期效果.
ko.renderTemplateX = function(name, data){
// create temporary container for rendered html
var temp = $("<div>");
// apply "template" binding to div with specified data
ko.applyBindingsToNode(temp[0], { template: { name: name, data: data } });
// save inner html of temporary div
var html = temp.html();
// cleanup temporary node and return the result
temp.remove();
return html;
};
Run Code Online (Sandbox Code Playgroud)
看看这个小例子:http://jsfiddle.net/6s4gq/
更新:
最初它是ko.renderTemplate方法,但在Knockout中有相同名称的内置方法.覆盖ko.renderTemplate可能会停止处理您的应用程序,尤其是在您使用template绑定时.小心!
| 归档时间: |
|
| 查看次数: |
3561 次 |
| 最近记录: |