简体   繁体   English

如何获取模板html文本

[英]How can I get template html text

I know that knockout has method ko.renderTemplate() https://www.strathweb.com/2012/08/knockout-js-pro-tips-working-with-templates/我知道淘汰赛有方法 ko.renderTemplate() https://www.strathweb.com/2012/08/knockout-js-pro-tips-working-with-templates/

So I have next code:所以我有下一个代码:

var templateHtmlText = ko.renderTemplate('packageView', package[0])

but it returns "<!--[ko_memo:5c90e0726c7e506b]-->"但它返回"<!--[ko_memo:5c90e0726c7e506b]-->"

How can I get ready html code in variable with applied bindings?如何使用应用绑定在变量中准备好 html 代码?

You need to supply an element for knockout to bind to ( elem in this example).您需要为要绑定到的淘汰赛提供一个元素(在本例中为elem )。 RenderTemplate then binds the data ( obs in this example) to this element. RenderTemplate 然后将数据(在本例中为obs )绑定到这个元素。 If you want to store the result in a variable, then grab the elements innerHTML :如果要将结果存储在变量中,请获取元素innerHTML

 var obs = ko.observable({demo:'demo text'}); var elem = document.createElement('div'); ko.renderTemplate('template', obs, null, elem); console.log(elem.innerHTML);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script> <script type="text/html" id="template"> <span data-bind="text: demo"></span> </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在ExtJS中获取任何HTML的渲染文本? - How can I get rendered text of any HTML in ExtJS? 如何在Get中发送许多html文本? jQuery,JavaScript - How can I send Many html text in Get? JQuery, Javascript 如何获取段落文本,然后将其转换为HTML中的整数 - How can i get the text of a paragraph and then turn it into an integer in HTML 如何从HTML文档中仅获取文本(无标记)? - How can I get the text only (no tags) from a HTML document? 如何获取html树的#text节点数组 - How can I get array of #text nodes of the html tree 如何在Browserify中使用html模板 - How can I require a html template with Browserify 将text.js与require.js结合使用时,如何创建一个模板html文件,但分别获取每个脚本模板? - Using text.js with require.js how can I create one template html file but grab each script template individually? 如何使用另一个模板上的html更改流星中的模板主体? - How can I change the body of template in meteor with a html on another template? 如何从呈现的 HTML 模板呈现 JS 模板? - How can I render a JS template from a rendered HTML template? 如何使用 JS 获取 flask 变量的刷新值并每 30 秒显示在 HTML 模板中? - How can i get refreshed value of flask variable with JS and show in HTML template every 30 seconds?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM