亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

形式的 ValueChanges 中的 Angular Debounce

形式的 ValueChanges 中的 Angular Debounce

隔江千里 2023-05-19 15:13:05
我有一個名為 filteredUserNames 的列表,其中包含很多用戶。每次我更改表單上的值時,它都會啟動一個新的數據過濾器。我知道要延遲時間以便不是每個角色都會觸發我需要使用去抖動的新過濾器,但我不確定在哪里添加它。它應該在價值變化訂閱中嗎?還有什么是正確的實施方法?我有searchString = new BehaviorSubject("");searchString$ = this.searchString.asObservable();在構造函數中this.myForm = this.fb.group({  searchString: [""],});this.myForm.controls.searchString.valueChanges.subscribe((val) => {// SHOULD THE DEBOUNCE GO HERE ?? //  this.searchString.next(val);  }在 ngOnInitthis.searchString$.subscribe((searchTerm) => {      console.log(this.userNames);      if (this.userNames !== undefined) {      this.filteredUserNames = this.userNames.filter(        (userName) =>          userName.searchTerms            .toLowerCase()            .indexOf(searchTerm.toLowerCase()) !== -1      );      };    });
查看完整描述

1 回答

?
守候你守候我

TA貢獻1802條經驗 獲得超10個贊

試試這個,你可以添加 distinctUntilChanged來忽略相似的值,并為你的副作用點擊運算符,在你的情況下,它會為你的 behaviorSubject 發出新的值


import { tap, distinctUntilChanged, debounceTime} from 'rxjs/operators';

...


this.myForm.controls.searchString.valueChanges.pipe(

   debounceTime(400),

   distinctUntilChanged(),

   tap((val) => this.searchString.next(val))    

 ).subscribe()


查看完整回答
反對 回復 2023-05-19
  • 1 回答
  • 0 關注
  • 159 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號