简体   繁体   English

渲染淘汰模板“内联”

[英]Render knockout templates 'inline'

The tile may be a bit confusing but let me show you what I want.瓷砖可能有点混乱,但让我告诉你我想要什么。

Consider the following knockout templates:考虑以下淘汰模板:

<script type="text/html" id="my-template">
    <span data-bind="text:text"></span>
</script>

<div data-bind="foreach: nodes">
    <!-- ko template: {name: 'my-template'}--><!-- /ko-->
</div>

This will result in something like:这将导致类似:

<div>
    <span>Text </span>
    <span class="bold">bold</span>
    <span>.</span>
</div>

To avoid the space the HTML is given to spans on e new line, I want to rander the spans on the same line, like this:为了避免在新行上为跨度分配 HTML 的空间,我想在同一行上对跨度进行排序,如下所示:

<div>
    <span>Text </span><span class="bold">bold</span><span>.</span>
</div>

I am aware of the font-size:0 workaround, but that is not the fix I'm looking for because I can not oversee the issues for projects which are already using the current rendering templates.我知道 font-size:0 解决方法,但这不是我正在寻找的解决方法,因为我无法监督已经使用当前渲染模板的项目的问题。

I've hit this issue before where the resulting empty text nodes created were causing bigger issues than just whitespace on screen.在创建的结果空文本节点导致比屏幕上的空白更大的问题之前,我已经遇到了这个问题。

It's caused by the fact that anything within the relevant sections (both the template and the <div> in your case) is part of that section, and will be repeated alongside the actual content you're interested in. That includes all the whitespace before/after the <span> in your <script> template, and that before/after the inline comment knockout directives.这是因为相关部分中的任何内容(在您的情况下是模板和<div> )都是该部分的一部分,并且将与您感兴趣的实际内容一起重复。这包括之前的所有空格/ 在<script>模板中的<span>之后,以及在内联注释淘汰指令之前/之后。

One quick fix is to ensure your template doesn't have any extraneous whitespace in it outside of your <span> :一种快速解决方法是确保您的模板在<span>之外没有任何多余的空白:

<script type="text/html" id="my-template"><span data-bind="text:text"></span></script>

In your case you also need to remove the whitespace from inside your repeating <div> contents too:在您的情况下,您还需要从重复的<div>内容中删除空格:

<div data-bind="foreach: nodes"><!-- ko template: {name: 'my-template'}--><!-- /ko--></div>

For example:例如:

 var vm = { nodes: ko.observableArray() } vm.nodes.push({ text: 'one' }); vm.nodes.push({ text: 'two' }); vm.nodes.push({ text: 'three' }); ko.applyBindings(vm);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script> <h2>Without whitespace</h2> <script type="text/html" id="my-template"><span data-bind="text:text"></span></script> <div data-bind="foreach: nodes"><!-- ko template: {name: 'my-template'}--><!-- /ko--></div> <h2>With whitespace</h2> <script type="text/html" id="my-template-ws"> <span data-bind="text:text"></span> </script> <div data-bind="foreach: nodes"> <!-- ko template: {name: 'my-template-ws'}--><!-- /ko--> </div>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM