我正在使用 PrimeNG 的 inputMask
字段
<p-inputMask name="maskValue" [(ngModel)]="attr.attributeValue" mask="aa/99" placeholder="aa/99"></p-inputMask>
{{attr.attributeValue}}
但如果我在文本框中输入 a
并聚焦,该文本框将被清除,但 ng-model attr.attributeValue
仍保持值 a_/__
。我们怎样才能同时清除 ng-model?
最佳答案
通过查看 source of inputMask ,您可以看到有一个名为 filled
的 bool 属性。
因此我们可以通过 ViewChild
访问该属性的值以了解输入是否已填充,并使用 onBlur
事件,您可以在该事件中重新初始化您的模型未填满。
HTML
<p-inputMask #myInputMask name="maskValue" [(ngModel)]="attr.attributeValue" mask="aa/99" placeholder="aa/99" (onBlur)="isInputMaskFilled($event)"></p-inputMask>
TS
@ViewChild('myInputMask') myInputMask: InputMask;
isInputMaskFilled(event) {
if(!this.myInputMask.filled) {
this.attr = { attributeValue: '' };
}
}
参见 StackBlitz
关于angular - p-inputMask ng-model 未清除无效值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50286780/