本文介绍了两次迭代相同的数组时,手柄会打印错误的东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$ b
var source = document.getElementById(entry-template)。innerHTML; var template = Handlebars.compile(source); var context = {colors:['red','blue','green']}; var html = template(context); document.getElementById(output)。innerHTML = html;
< script src =https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.11/handlebars.js>< / script>< script id =entry-templatetype =text / x-handlebars-template> output:{{#colors}} {{#.. / colors}} color1:{{../this}} color2:{{这个}}; {{/../colors}} {{/ colors}}< / script>< pre id =output> < / pre>
这是
解决方案
我还不确定是什么原因导致它的行为,但你可以通过使用块参数来修复它。
var source = document.getElementById(entry-template)。innerHTML; var template = Handlebars.compile(source); var context = {colors: ['red','blue','green']}; var html = template(context); document.getElementById(output)。innerHTML = html;
< script src =https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0。 11 / handlebars.js>< / script>< script id =entry-temp color =color1} color2:{color =color1} color2:{color =color1} color2:{} {颜色2}}; {{/../colors}} {{/ colors}}< / script>< pre id =output> < / pre>
Trying to print out all combinations of 2 items from an array.
var source = document.getElementById("entry-template").innerHTML;
var template = Handlebars.compile(source);
var context = {
colors: ['red', 'blue', 'green']
};
var html = template(context);
document.getElementById("output").innerHTML = html;
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.11/handlebars.js"></script>
<script id="entry-template" type="text/x-handlebars-template">
output:
{{#colors}}
{{#../colors}}
color1: {{../this}} color2: {{this}};
{{/../colors}}
{{/colors}}
</script>
<pre id="output">
</pre>
Here is a Codepen Demo
解决方案
I'm not yet sure what's causing it to behave that way, but you can fix it by using block parameters.
var source = document.getElementById("entry-template").innerHTML;
var template = Handlebars.compile(source);
var context = {
colors: ['red', 'blue', 'green']
};
var html = template(context);
document.getElementById("output").innerHTML = html;
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.11/handlebars.js"></script>
<script id="entry-template" type="text/x-handlebars-template">
output:
{{#colors as |color1|}}
{{#../colors as |color2|}}
color1: {{color1}} color2: {{color2}};
{{/../colors}}
{{/colors}}
</script>
<pre id="output">
</pre>
这篇关于两次迭代相同的数组时,手柄会打印错误的东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!