访问 repeat.for 中的 ref 元素

Access ref elements inside a repeat.for

我在 repeat.for 中有一个对象的 ref,看起来像这样

<tr repeat.for="machine of machines">
    <div with.bind="machine" ref="pb">...</div>
</tr>

Aurelia Inspector 显示机器对象下的 ref:

我试图在 attached() 中像这样访问它:

attached() {
    console.log(this.machines[0].pb)
}

但是这当然会 return 一个错误,因为 pb 在对象上不存在。

通常当我尝试访问我的 Typescript 代码中的引用绑定元素时,我知道我需要先初始化它们。但是现在我该怎么做,因为它在另一个对象中?

找到答案了。将打字稿中的对象定义为

pbElements = [];

并在 html 中设置:

<tr repeat.for="machine of machines">
    <div ref="pbElements[$index]">...</div>
</tr>