0
我試圖模仿用密碼完成的操作,無論用戶輸入什麼內容,它都會被•••••••
代替,除了我想對數字執行此操作並僅顯示最後一個四個數字。到目前爲止,我的@Directive
中的事件捕獲鍵擊,但實際上並未發射或傳播流(輸入中)的任何更改以更改值。Angular 4將表單輸入值更改爲#綁定時
Ex。 #####1234
<input id="indTaxId" type="text" name="indTaxId"
[(ngModel)]="payLoadService.individual.taxId"
required maxlength="9" pattern="^[0-9]{9}$"
[lastFourDirective]="payLoadService.individual.taxId"
(lastFourDirectiveOutput)="payLoadService.individual.taxId"
/>
最後four.directive.ts
@Directive({
selector: '[lastFourDirective]'
})
export class LastFourDirective implements OnInit {
private el: HTMLInputElement;
constructor(
private elementRef: ElementRef
) {
this.el = this.elementRef.nativeElement;
}
ngOnInit() {
}
@Input() lastFourDirective: string;
@HostListener('blur', ['$event.target.value'])
onBlur(value) {
console.log("blur", value);
return this.lastFourDigits(value)
}
@HostListener('keyup', ['$event.target.value'])
onKeyup(value) {
console.log("keyup", value);
return this.lastFourDigits(value)
}
private lastFourDigits(taxId: string) {
return taxId.replace(/\d(?=\d{4})/g, '#')
}
}
我怎樣才能做到這一點?
PS:我沒有使用formControl,上面是我的輸入樣本。
周圍午夜我有同樣的想法,並認爲我現在只是在我的組件中做並在需要時提取它。這做到了。謝謝@AshishRanjan – Diego
歡迎:) –