在Aurelia Compose的视图模型中访问模型

Cal*_*ton 3 javascript aurelia

因此,根据标题动态渲染UI到基于数据的DOM 这里(向下滚动位).

<template repeat.for="item of items">
    <compose model.bind="item" view-model="widgets/${item.type}"></compose>
</template>
Run Code Online (Sandbox Code Playgroud)

如果你更简单地说:

<compose model.bind="item" view-model="itemViewModel.js"></compose>
Run Code Online (Sandbox Code Playgroud)

如果我有a itemViewModel.html和a itemViewModel.js,它们都会成功加载.但是,如何访问绑定模型itemViewModel.js

我试过用了bindable.

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

export class ItemViewModel {
    @bindable model;

    constructor() {
        console.log("using bindable", this);
    }
}
Run Code Online (Sandbox Code Playgroud)

这可能吗?

Cor*_*liu 5

<compose model.bind="item"activate(model)在您的viewmodel中调用一个方法并为您提供item.

// the model received here is the *item* from the above <compose
activate(model){
   this.model = model;
}
Run Code Online (Sandbox Code Playgroud)

如果你想在绑定中传递多个模型,你可以做到

<compose model.bind="{item: item, value: value}"
Run Code Online (Sandbox Code Playgroud)

然后你得到:

activate(model){
    this.item = model.item;
    this.value = model.value;
}
Run Code Online (Sandbox Code Playgroud)