angular6 input節流


一直以為   pipe(debounceTime(1000), distinctUntilChanged())  不起作用

原因:使用方法錯誤

<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 = [];
            }
        })
    }

經查詢資料后發現正確的使用方法 :以下二種:

方法一:

 <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 = ["暫無匹配數據"];
                    }
                })
            })

 

方法二:

 <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 = ["暫無匹配數據"];
                    }
                })
            })

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM