[英]Two-Way binding in an Aurelia Custom Attribute
更新:看起來這是一個已知的錯誤: https : //github.com/aurelia/template/issues/253
我將其留在此處以供參考/可搜索性目的。
input-mask.ts (完整的代碼可以在這里看到)
@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...
}
carrier.html
<input input-mask="mask.bind: carrier.airbillMask; unmasked-value.bind: airbill"
value.bind="formattedAirbill"/>
更新:要變通解決此錯誤,更改為
unmasked-value.two-way
,綁定將起作用。
載體
@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
事件,並顯示控制台語句,該語句指示從自定義屬性內部更新了未屏蔽的Value:
unmaskedValue從自定義屬性內部更新
但是(當輸入失去焦點時)控制台語句說,當我退出輸入框時, airbill
文件上的airbill
已更新不會觸發:
這不會觸發:
console.log('Airbill已設置!');
這似乎向我表明,綁定實際上不是雙向的。
我該如何進行雙向綁定? 這樣,當我在自定義屬性內更新unmaskedValue
時,它將更新視圖模型中的綁定值嗎?
注意:作為一種解決方法,我可以將unmasked-value.bind
更改為方法調用( on-unmasked-value-changed.call="onUnmaskedValueChanged($event)
)並更新該方法中的值。所以我不t需要此功能,但我想知道將來是否可以使用。
此已知錯誤已於2016年3月15日修復並關閉https://github.com/aurelia/templating/issues/253#issuecomment-189394955
嘗試使用默認值初始化變量unmaskedValue。 嘗試null,undefined,''等。 我以前做過,但是我不記得是哪個版本(肯定是beta版)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.