Aurelia自定义元素 - $ parent undefined

时间:2017-10-16 09:47:10

标签: javascript aurelia aurelia-templating

我有转发器的模板:

<template repeat.for="i of 2">
    <template repeat.for="j of 2">
        <p>${ $parent.$index } - ${ $index }</p>
    </template>
</template>

打印结果:

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>

结果仅为:

-
-
-
-

有没有办法透明地将$ parent和$ index传播到child-item

更新

在尝试了一些建议后,我最接近的是:

<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内联的内容都无法实现。

2 个答案:

答案 0 :(得分:2)

这样的事情会起作用

子-item.ts:

import { customElement, bindable } from 'aurelia-framework';

    @customElement('child-item')
    export class ChildItem {
      @bindable index;
    }

儿童item.html

<template>
    <p>${ index }</p>
</template>
带转发器的

模板:

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

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

答案 1 :(得分:1)

您需要使用数据绑定将其传入。将let testList = [(1,"c");(2,"a");(1,"b")][(1, ["c";"b"]; (2, ["a"])]可绑定属性添加到let rec toRel xs = match xs with | (a,b)::rest -> (a,[b])::toRel rest | _ -> [] 视图模型。