在aurelia中使用model.bind进行无视图模型合成

在aurelia中使用model.bind进行无视图模型合成,aurelia,Aurelia,我有一个结构,其中主视图组成一个部分视图,该部分视图在中继器中组成另一个部分视图 例如: 主视图 <template> <h1>${factory.name}</h1> <div class="column"> <compose view="./cars.html"></compose> </div> </template> <template repe

我有一个结构,其中主视图组成一个部分视图,该部分视图在中继器中组成另一个部分视图

例如:

主视图

<template>
    <h1>${factory.name}</h1>
    <div class="column">
        <compose view="./cars.html"></compose>
    </div>
</template>
<template repeat.for="car of factory.cars">
    <compose view="./specifications.html model.bind="{test: 'abc}"></compose>
</template>
<template repeat.for="car of factory.cars">
    <h1>${$parent.$parent.factory.name} - ${car.name}</h1>
    ${test}
</template>
<template>
    <h1>${factory.name}</h1>
    <div>
        <compose view="./cars.html"></compose>
    </div>
</template>

${factory.name}
汽车视图

<template>
    <h1>${factory.name}</h1>
    <div class="column">
        <compose view="./cars.html"></compose>
    </div>
</template>
<template repeat.for="car of factory.cars">
    <compose view="./specifications.html model.bind="{test: 'abc}"></compose>
</template>
<template repeat.for="car of factory.cars">
    <h1>${$parent.$parent.factory.name} - ${car.name}</h1>
    ${test}
</template>
<template>
    <h1>${factory.name}</h1>
    <div>
        <compose view="./cars.html"></compose>
    </div>
</template>


仅使用html文件进行撰写时,引用的html文件的视图模型与撰写元素的放置位置相同。换句话说,它继承父视图的视图模型。所以你不需要提供模型

主视图

<template>
    <h1>${factory.name}</h1>
    <div class="column">
        <compose view="./cars.html"></compose>
    </div>
</template>
<template repeat.for="car of factory.cars">
    <compose view="./specifications.html model.bind="{test: 'abc}"></compose>
</template>
<template repeat.for="car of factory.cars">
    <h1>${$parent.$parent.factory.name} - ${car.name}</h1>
    ${test}
</template>
<template>
    <h1>${factory.name}</h1>
    <div>
        <compose view="./cars.html"></compose>
    </div>
</template>

${factory.name}
cars.html

<template>
  <div  repeat.for="car of factory.cars">
    <compose view="./specifications.html"></compose>
  </div>
</template>
<template>
    <h1>${factory.name} - ${car.name}</h1>
</template>

specifications.html

<template>
  <div  repeat.for="car of factory.cars">
    <compose view="./specifications.html"></compose>
  </div>
</template>
<template>
    <h1>${factory.name} - ${car.name}</h1>
</template>

${factory.name}-${car.name}

看看这个。

Jeff G所说的是正确的,但对于我的特定用例,解决我问题的是创建一个简单的视图模型,我可以用于所有合成。它看起来像:

export class Main {
    public parentFactory;

    public activate(data) {
        this.parentFactory= data;
    }
}
在我看来

    <compose
        view="./car.html"
        view-model="../view-models/main"
        model.bind="$parent.$parent.factory">
    </compose>


AFAIK,model.bind指示引擎将其传递给视图模型的激活,如果您想为其设置绑定上下文,请尝试使用.bind,然后您将失去由repeat设置的汽车上下文。我会添加一个VM并在那里注入一个工厂tbh。确实如此,但如果每个car对象都有自己的
工厂,这将不起作用。然后,在
cars.factories
上执行
repeat.for
以编写
specifications.html
时,我会丢失初始的
factory
对象。你说“每个car对象都有自己的工厂”是什么意思?