angular6 input节流

一直觉得   pipe(debounceTime(1000), distinctUntilChanged())  不起做用this

缘由:使用方法错误spa

<input type="text" [(ngModel)]='globalSearchWord' placeholder="请输入搜索关键词" (keyup)='globalSearch()'>

 

 globalSearch(v) {
        this.showErrBox = false;

        this.indexService.globalSearch(this.globalSearchWord).pipe(debounceTime(1000), distinctUntilChanged()).subscribe(data => {
            if (data.code == '0001') {
                this.options = data.data;
            } else {
                let that = this;
                // setTimeout(function () {
                //     that.showErrBox = false;
                // }, 2000)
                this.options = [];
            }
        })
    }

经查询资料后发现正确的使用方法 :如下二种:code

方法一:orm

 <input type="text" [formControl]="word">
 this.word = new FormControl('');
 this.word.valueChanges
            .pipe(
                debounceTime(500),
                distinctUntilChanged()
            ).subscribe(val => {
                this.showErrBox = false;
                this.indexService.globalSearch(val).subscribe(data => {
                    if (data.code == '0001') {
                        this.options = data.data;
                    } else {
                        let that = this;
                        this.options = ["暂无匹配数据"];
                    }
                })
            })

 

方法二:blog

 <input #questionInput placeholder="请输入搜索关键词" nz-input [(ngModel)]="globalSearchWord">
 this.input$ = fromEvent(this.questionInput.nativeElement, 'input')
            .pipe(
                debounceTime(500),
                distinctUntilChanged()
            ).subscribe(val => {
                this.showErrBox = false;
                this.indexService.globalSearch(this.globalSearchWord).subscribe(data => {
                    if (data.code == '0001') {
                        this.options = data.data;
                    } else {
                        let that = this;
                        this.options = ["暂无匹配数据"];
                    }
                })
            })
相关文章
相关标签/搜索