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

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

angular2函數去抖的問題

angular2函數去抖的問題

呼啦一陣風 2018-11-22 18:15:54
在angular2環境中寫了一個自定義的debounce函數,因為使用了服務中的本地變量,所以沒有使用閉包在傳入函數作為參數時,遇到了兩個問題如何給傳入函數加入參數如何將傳入函數的作用域綁定在聲明他的component中代碼如下debounceTime(fn, delay) {if (isUndefined(this.timeout)) {this.timeout = setTimeout(() => {            fn();        }, delay);    } else {        clearTimeout(this.timeout);        this.timeout = setTimeout(() => {            fn();        }, delay);    }} emitSubmitDate(event) {    this.submit_data['search'] = event.value;    this.globalFuncService.setInfoListSource(this.request_type, this.request_url,                          this.submit_data);    this.globalFuncService.emitInfoListSource();}staffSearch(event) {    this.globalFuncService.debounceTime(this.emitSubmitDate, 500);}希望將event作為參數傳入emitSubmit函數,然后再將帶有event的emitSubmit傳入debounce實際遇到的問題是this的變化導致各種undefined本人js不是很好。。。希望各位多多指教
查看完整描述

1 回答

?
慕斯709654

TA貢獻1840條經驗 獲得超5個贊

staffSearch(event) {

    this.globalFuncService.debounceTime.apply(this, [() => this.emitSubmitDate(event), 500]);

}

apply變更this指向,也可以用call或bind

傳event則通過一個閉包實現


PS: angular2本身就依賴了Rx,何必自己寫debounceTime....


查看完整回答
反對 回復 2018-12-30
  • 1 回答
  • 0 關注
  • 603 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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