简体   繁体   中英

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

I need to trim input on blur feature on input box and for that using ng2-trim-directive library

angular v 6.1.10 ng2-trim-directive v 2.3.0

we need to write trim="blur" in reactive form input to apply this feature, it works fine as expected and it trim input after the blur.

it also trim if all whitespace and display error on the required field. BUT facing one strange issue. when user input only a single letter, it throws the same error as required.

interestingly it works fine when writing 2 letters and removes the later from the input box.

Will you please check this issue?

check this demo .

enter 'a' . it throws error

now if I write 'aa' and remove one letter, it does not throw any error.

why both are behaving differently?

someone can look into this issue and let me know how to fix that?

the issue was in the library, so change the code on 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);
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM