双向绑定不适用于Aurelia自定义属性
我正在努力实现。我使用的是TypeScript,所以我尽可能地修改了他的代码。由于某些原因,我的虚拟机上的双向绑定不适用于Aurelia自定义属性,aurelia,Aurelia,我正在努力实现。我使用的是TypeScript,所以我尽可能地修改了他的代码。由于某些原因,我的虚拟机上的isDirty标志没有更新。以下是我所拥有的: agency.ts-(我的虚拟机) agency.html-(我的观点) 因此,通过日志记录,我可以看到正在调用updateSource()code,并且正在设置self.value标志。但是,在我的视图中带有按钮的div从不显示,并且${isDirty}模板始终显示false。看起来,dirty.two-way=“isDirty”不像它应该的
isDirty
标志没有更新。以下是我所拥有的:
agency.ts-(我的虚拟机)
agency.html-(我的观点)
因此,通过日志记录,我可以看到正在调用updateSource()
code,并且正在设置self.value
标志。但是,在我的视图中带有按钮的div
从不显示,并且${isDirty}
模板始终显示false。看起来,dirty.two-way=“isDirty”
不像它应该的那样是双向绑定
我哪里做错了
更新
import {bindable, bindingMode} from 'aurelia-framework';
export class DirtyCustomAttribute{
private view: any;
private bindings: any[] = [];
@bindable({defaultBindingMode: bindingMode.twoWay }) value: any;
static inject = [Element]
constructor(public element : HTMLElement) {}
created(view) {
this.view = view;
}
bind() {
this.bindings = this.view.bindings
.filter(b => b.mode === bindingMode.twoWay && this.element.contains(b.target));
let i = this.bindings.length;
let self = this;
while (i--) {
let binding = this.bindings[i];
binding.dirtyTrackedUpdateSource = binding.updateSource;
binding.updateSource = function (newValue) {
this.dirtyTrackedUpdateSource(newValue);
if (!self.value) {
self.value = true;
}
}
}
}
unbind() { // See code in referenced link }
}
因此,它可能是由于撰写,所以我将添加额外的代码,以显示这是如何撰写的
shell.ts
export class Shell
{
public agencies : IAgency[] = []; //loaded in activate
public tablist : any[] = [
{title: "Detail", vm: PLATFORM.moduleName("./agency") },
...
];
public selectedAgency : IAgency = null;
public selectedVM : any = this.tablist[0];
}
shell.html
<template>
...
<compose view-model.bind="selectedVM.vm" model.bind="selectedAgency"></compose>
</template>
activate(model){
this.agency = model;
...
}
关于自定义属性,我没有发现您的代码有任何错误。我制作了一个代码沙盒,你的代码似乎在做你想做的事情。请看一看,以便您可以观察是否有任何差异来确定原因。因此,这一定与我使用的
有关。为了简单起见,我省略了这一部分,但是代理机构虚拟机正在另一个虚拟机中组成。我试图在您的codesandbox
示例上测试这一点,但我似乎无法让
在那种环境中工作。我过去没有幸获得双向绑定来与compose一起工作。如果不可能,您可能希望连接事件聚合器或服务。文档中没有提到可以传回数据。
<template>
...
<compose view-model.bind="selectedVM.vm" model.bind="selectedAgency"></compose>
</template>
activate(model){
this.agency = model;
...
}