我知道我可以在我的视图模型中创建对我的组件的引用,如下所示:
html的:
<template>
<mdfield view-model.ref="ref"></mdfield>
</template>
.TS:
export class Vm {
ref: any;
test(){
console.log(this.ref);
}
}
这有效,但如果我动态创建组件,语法是什么?像这样:
<template>
<div repeat.for="field of fields">
<mdfield view-model.ref="<what goes here?>"></mdfield>
</div>
</template>
我想我想将它们添加到我的viewmodel中的数组中供以后参考,但是如何?
$index
为您提供当前的repeat.for
指数。因此,如果要将视图模型引用添加到数组:
<div repeat.for="field of fields">
<mdfield view-model.ref="refArray[$index]"></mdfield>
</div>