Reference dynamically created component in Aurelia

时间:2017-12-18 08:27:45

标签: aurelia aurelia-framework

I know I can create a reference to my component in my view model like this:

.html:

board.defaultAxes.y

.ts:

<template>
  <mdfield view-model.ref="ref"></mdfield>
</template>

This works, but what is the syntax if I'm creating the components dynamically? Like this:

export class Vm {
  ref: any;

  test(){
   console.log(this.ref);
  }
}

I guess I want to add them to an array in my viewmodel for later reference, but how?

1 个答案:

答案 0 :(得分:1)

$index为您提供repeat.for的当前索引。因此,如果要将视图模型引用添加到数组中:

<div repeat.for="field of fields">
    <mdfield view-model.ref="refArray[$index]"></mdfield>
</div>