在我的angular 6应用程序中,我订阅了一个特定表单字段中的值更改,并且我想避免如果输入<3个字符,则该值更改将被调用,这就是值更改:
this.posForm.controls["customer"].valueChanges
.pipe(
tap(() => (this.load = true)),
debounceTime(500),
switchMap(val => this.customerService.getCustomers(this.basicService.handleValueAutocomplete(val, "lastName"))),
tap(() => (this.load = false))
)
.subscribe(customers => {
this.matchingCustomers.next(customers);
});
所以我尝试使用指令,但是总是会触发值更改:
@Directive({
selector: "[blockValueChangesDirective]"
})
export class BlockValueChangesDirective {
inputElement: ElementRef;
constructor(el: ElementRef) {
this.inputElement = el;
}
@HostListener("keypress", ["$event"])
onKeyPress(event) {
if (this.inputElement.nativeElement.value.length < 3) {
console.log(this.inputElement.nativeElement.value.length + " < 3");
event.stopPropagation();
} else {
console.log(">= 3");
}
}
如果输入短于3个字符,是否有办法中断valueChanges内容?
p.s。我不想在.pipe内的每个运算符中添加条件。