diff --git a/compiler/generate/index.js b/compiler/generate/index.js index 841f87f17b..b7fb973375 100644 --- a/compiler/generate/index.js +++ b/compiler/generate/index.js @@ -410,7 +410,7 @@ export default function generate ( parsed, template ) { } const iteration = ${name}_iterations[i]; - ${name}_iterations[i].update( ${current.contextChain.join( ', ' )}, ${expression}[i] ); + ${name}_iterations[i].update( ${current.contextChain.join( ', ' )}, ${expression}[i]${node.index ? `, i` : ''} ); } for ( var i = ${expression}.length; i < ${name}_iterations.length; i += 1 ) { @@ -425,18 +425,28 @@ export default function generate ( parsed, template ) { for ( let i = 0; i < ${name}_iterations.length; i += 1 ) { ${name}_iterations[i].teardown(); } + + ${name}_anchor.parentNode.removeChild( ${name}_anchor ); ` ); const contexts = Object.assign( {}, current.contexts ); + const contextChain = current.contextChain.concat( node.context ); + contexts[ node.context ] = true; + if ( node.index ) { + // not strictly a context, but we can treat it as such + contextChain.push( node.index ); + contexts[ node.index ] = true; + } + current = { useAnchor: false, name: renderer, target: 'target', contexts, - contextChain: current.contextChain.concat( node.context ), + contextChain, initStatements: [], updateStatements: [], diff --git a/test/compiler/each-block-indexed/_config.js b/test/compiler/each-block-indexed/_config.js new file mode 100644 index 0000000000..34640e18cb --- /dev/null +++ b/test/compiler/each-block-indexed/_config.js @@ -0,0 +1,6 @@ +export default { + data: { + animals: [ 'adder', 'blue whale', 'chameleon' ] + }, + html: `

0: adder

1: blue whale

2: chameleon

` +}; diff --git a/test/compiler/each-block-indexed/main.svelte b/test/compiler/each-block-indexed/main.svelte new file mode 100644 index 0000000000..5c1c74da66 --- /dev/null +++ b/test/compiler/each-block-indexed/main.svelte @@ -0,0 +1,3 @@ +{{#each animals as animal, i}} +

{{i}}: {{animal}}

+{{/each}}