简体   繁体   English

模糊指令后修剪:angular2-trim-directive,即使只有一个字母也抛出错误

[英]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 我需要修剪输入框上的模糊功能的输入,并为此使用ng2-trim-directive

angular v 6.1.10 ng2-trim-directive v 2.3.0 角度v 6.1.10 ng2-trim指令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. 我们需要在反应形式输入中写入trim =“ 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. 有趣的是,当写2个字母并从输入框中删除后者时,它可以正常工作。

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. 现在,如果我写“ aa”并删除一个字母,它不会引发任何错误。

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 问题出在库中,因此请更改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