2017-10-29 1 views
0

Der Versuch, alle Kombinationen von 2 Elementen aus einem Array auszudrucken.Lenker druckt falsches Objekt, wenn zweimal durch dasselbe Array getippt wird

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>

Hier ist ein Codepen Demo

+1

Possible Duplikat [../this gibt das Ansichtsobjekt innerhalb der inneren Schleife, wenn die Eltern und Kind den gleichen Wert haben] (https://stackoverflow.com/questions/40935524/ this-returns-the-view-object-inside-inner-loop-when-the-parent-und-kind-have) – 76484

Antwort

1

Ich bin noch nicht sicher, was es so verhalten verursacht, aber man kann es beheben, indem Blockparameter.

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>

+0

das hat dieses Problem gelöst, aber jetzt habe ich ein ähnliches Problem beim Hinzufügen einer dritten Schleife https: //stackoverflow.com/questions/47041164/handlebars-prints-wrong-thing-when-iterating- through-same-array-thrice – stackers

Verwandte Themen