Angular 2+ 和去抖動 - Angular 2+ and debounce

問題:

In AngularJS I was able to debounce a model by using ng-model options.在 AngularJS 中,我能夠通過使用 ng-model 選項來消除模型的抖動。

ng-model-options="{ debounce: 1000 }"

How can I debounce a model in Angular?如何在 Angular 中對模型進行去抖動?
I tried to search for debounce in the docs but I couldn't find anything.我試圖在文檔中搜索去抖動,但找不到任何東西。

https://angular.io/search/#stq=debounce&stp=1 https://angular.io/search/#stq=debounce&stp=1

A solution would be to write my own debounce function, for example:一個解決方案是編寫我自己的 debounce 函數,例如:

import {Component, Template, bootstrap} from 'angular2/angular2';

// Annotation section
@Component({
  selector: 'my-app'
})
@Template({
  url: 'app.html'
})
// Component controller
class MyAppComponent {
  constructor() {
    this.firstName = 'Name';
  }
    
  changed($event, el){
    console.log("changes", this.name, el.value);
    this.name = el.value;
  }

  firstNameChanged($event, first){
    if (this.timeoutId) window.clearTimeout(this.timeoutID);
    this.timeoutID = window.setTimeout(() => {
        this.firstName = first.value;
    }, 250)
  }
    
}
bootstrap(MyAppComponent);

And my html還有我的 html

<input type=text [value]="firstName" #first (keyup)="firstNameChanged($event, first)">

But I'm looking for a built in function, is there one in Angular?但我正在尋找一個內置函數,Angular 有嗎?


解決方案:

參考一: https://en.stackoom.com/question/2AU01
參考二: https://stackoom.com/question/2AU01
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章