![](/img/trans.png)
[英]Knockoutjs nested ViewModel / component binding with typescript doesn't work
[英]Aurelia with Typescript: binding using properties doesn't work
我在Aurelia中使用Typescript(2.0)创建了一个自定义元素,并且想绑定一些值,但是它不起作用。 这是我的示例:
myelement.html:
<template>
<div>${caption}</div>
<div>${unit}</div>
</template>
myelement.ts:
import { bindable } from "aurelia-framework";
export class MyElement {
// removing the @bindable doesn't change anything
@bindable public caption: string;
@bindable public unit: string;
}
用法:
<require from="./myelement"></require>
...
<myelement caption.bind="currentvalue" unit=" km"></myelement>
(在类中使用custom元素)将currentvalue属性定义为
public currentvalue: number = 11;
正确插入了myelement,但是占位符被替换为空文本(即,已删除),甚至是仅是文本的“ unit”。
对于“单位”占位符有效的工作是:
myelement.html:
<template bindable="unit">
<div>${caption}</div>
<div>${unit}</div>
</template>
在这种情况下, ${unit}
被替换为“ km”。
不知何故,Aurelia无法获得自定义元素的html和viewmodel之间的连接。 使用bindable属性进行绑定有效,但前提是未绑定到属性。 myelement.js文件已正确加载(我将SystemJS与AMD模块配合使用)。 我一直想念什么?
更新:
我将myelement.ts @bindable
更改为@bindable()
,现在已正确绑定了“单位”-模板中的占位符已正确替换为“ km”。 但是使用属性(在本例中为标题)进行绑定仍然无效。
更新2:终于让我工作了。 使用<require from="./myparentelement.html">
包含了父自定义元素(使用myelement),因此未附加其viewmodel ...
父自定义元素不是正确必需的: <require from="./myparentelement.html">
而不是<require from="./myparentelement">
并且我不得不使用@bindable()
而不是@bindable
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.