簡體   English   中英

模糊指令后修剪:angular2-trim-directive,即使只有一個字母也拋出錯誤

[英]Trim after blur directive : angular2-trim-directive , throw error even there is a single letter

我需要修剪輸入框上的模糊功能的輸入,並為此使用ng2-trim-directive

角度v 6.1.10 ng2-trim指令v 2.3.0

我們需要在反應形式輸入中寫入trim =“ blur”才能應用此功能,它可以按預期工作,並且在模糊后可以修剪輸入。

它還會修剪所有空格,並在必填字段上顯示錯誤。 但是面臨一個奇怪的問題。 當用戶僅輸入一個字母時,將引發與所需相同的錯誤。

有趣的是,當寫2個字母並從輸入框中刪除后者時,它可以正常工作。

您能檢查一下這個問題嗎?

檢查這個演示

進入一個' 。 它引發錯誤

現在,如果我寫“ aa”並刪除一個字母,它不會引發任何錯誤。

為什么兩者的行為不同?

有人可以調查這個問題,讓我知道如何解決?

問題出在庫中,因此請更改input-trim.directive.ts上的代碼

private updateValue(event: string, value: string): void {
        const currentValue = this.trim !== '' && event !== this.trim ? value : value.trim();
        const previousValue = this._value;
        let trimmedPreviousValue = '';
        if (Boolean(previousValue)) {
            trimmedPreviousValue = previousValue.trim();
        }

        this.writeValue(currentValue);
        const trimmedValue = this._value.trim();
        if (trimmedValue !== previousValue && (trimmedValue !== '' || trimmedPreviousValue !== '')) {
            this.onChange(this._value);
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM