当迭代两次相同的数组时,把手打印错误的东西

时间:2017-10-29 02:30:20

标签: javascript html handlebars.js

尝试从阵列中打印出2个项目的所有组合。

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>

这是Codepen Demo

1 个答案:

答案 0 :(得分:1)

我还不确定是什么导致它的行为方式,但您可以使用阻止参数来修复它。

&#13;
&#13;
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;
&#13;
<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>
&#13;
&#13;
&#13;