2017-10-16 72 views
0

我有模板,中继器:

​​

其中打印结果:

0 - 0 
0 - 1 
1 - 0 
1 - 1 

如果我使用自定义元素child-item用相同的模板:

<template> 
    <p>${ $parent.$index } - ${ $index }</p> 
</template> 

并使用child-item写我的原始示例:

<template repeat.for="i of 2"> 
    <child-item repeat.for="j of 2"></child-item> 
</template> 

结果只有:

- 
- 
- 
- 

有没有办法传播$家长和$指数透明的child-item

UPDATE

后尝试一些建议,最近我来到的是:

<template repeat.for="i of 2"> 
    <child-item repeat.for="j of 2" parent-index.bind="$parent.$index" index.bind="$index"></child-item> 
</template> 

child-item模板的样子:

<template bindable="parentIndex, index"> 
    <p>${ parentIndex } - ${ index }</p> 
</template> 

绑定$parent情况下直接与parent.bind="$parent"不工作。父索引必须直接绑定。通过这种方法,任何与$parent.$parent.$index内联的内容都无法实现。

回答

1

您需要使用数据绑定才能将其传入。将或index可绑定属性添加到child-item视图模型。

+0

import { customElement, bindable } from 'aurelia-framework'; @customElement('child-item') export class ChildItem { @bindable index; } 

儿童item.html

<template> <p>${ index }</p> </template> 

模板,中继器举个简单的例子? 'index.bind =“$ index”'似乎合乎逻辑,但当前绑定上下文的值应该是多少? 'parent.bind = “...”'? – Nenad

2

这样的事会工作

儿童item.ts:你能

<template> 
    <require from="./child-item"> 

    <child-item repeat.for="child of childred" index.bind="$index"></child-item> 
</template> 
+0

'$ index'通过'bind(bindingContext:Object,overrideContext:Object)'中的'overrideContext'传递给自定义元素。所以'$ index'默认工作,不需要手动绑定。问题是如何使'$ parent'可用。 – Nenad

+0

从代码中不清楚你需要'$ parent'作为什么? 'bindingContext'是父类,虽然 – classicalConditioning

+0

我修改了问题,使其更加清晰,并添加更新后,我尝试了你和@ ashley授予建议。我也必须按照你的建议绑定索引。 – Nenad