UPDATE:看起来这是一个已知的错误:https://github.com/aurelia/templated/issues/253 https://github.com/aurelia/templating/issues/253
我将其留在这里是为了参考/可搜索的目的。
代码:
输入掩码.ts(完整代码可见here https://gist.github.com/Vaccano/e532610df4bff3211eb1)
@customAttribute('input-mask')
@inject(Element)
export class InputMaskCustomAttribute {
@bindable({ defaultBindingMode: bindingMode.twoWay,
changeHandler: 'onUnmaskedValueChanged' })
unmaskedValue: any;
onUnmaskedValueChanged(newValue, oldValue) {
console.log('unmaskedValue updated from inside the custom attribute');
}
@bindable
mask: string;
attached() {
this.eventTarget.on('focusout', (e: any) => {
this.unmaskedValue = (<any>$(this.element)).cleanVal()
this.fireEvent(e.target, 'input');
});
}
// Code for constructor, fireEvent and to setup the mask...
}
运营商.html
<input input-mask="mask.bind: carrier.airbillMask; unmasked-value.bind: airbill"
value.bind="formattedAirbill"/>
UPDATE:要解决此错误,请更改为unmasked-value.two-way
并且绑定将会起作用。
载体.ts
@bindable({ defaultBindingMode: bindingMode.twoWay})
carrier: EntityInterfaces.ICarrier;
@bindable({ defaultBindingMode: bindingMode.twoWay })
formattedAirbill: string;
@bindable({ defaultBindingMode: bindingMode.twoWay, changeHandler: 'onAirbillChanged' })
airbill: string;
onAirbillChanged() {
console.log('Airbill was set!');
}
问题:
数据似乎流入@bindable
变量就好了。当掩码发生变化时,自定义属性中的值也会发生变化。
但如果在自定义属性内部进行更改,它似乎不会流出。
示例场景:当我编辑输入框中的值并退出输入后,focusout
事件触发,并且控制台语句指示未屏蔽的值已从自定义属性内部打印更新:
unmaskedValue 从自定义属性内部更新
但是(当输入失去焦点时)控制台声明说airbill
当我退出输入框时,载体上的 ts 文件已更新不会触发:
这不会触发:
console.log('空运账单已设置!');
这似乎向我表明绑定并不是真正的双向。
问题:
我怎样才能使这种绑定成为双向的?这样当我更新的时候unmaskedValue
在自定义属性内它会更新视图模型中的绑定值吗?
注意:作为解决方法,我可以更改unmasked-value.bind
是一个方法调用(on-unmasked-value-changed.call="onUnmaskedValueChanged($event)
)并更新该方法中的值。所以我不需要这个来工作。但我想知道将来是否可以使用。