我正在嘗試創建類似isTyping或不是,因此我必須檢測用戶停止鍵入的時間多少爲3秒,因此我可以做我的東西,當他打字時我都已檢測到使用這個:檢測用戶是否停止鍵入Angular 2
ngOnChanges(searchValue: string) {
if (!this.searchChangeObserver) {
Observable.create(observer => {
this.searchChangeObserver = observer;
}).debounceTime(3000)
.subscribe((data) => {
if (data) {
typingStuff();
}
else {
notTypingStuff();
}
});
this.searchChangeObserver.next(searchValue);
}
}
}
所以現在我必須檢測當用戶停止鍵入做notTypingStuff();
有沒有簡單的方法來做到這一點?
編輯
我也是用這個:
constructor(){
this.modelChanged
.debounceTime(300)
.subscribe((data) => {
if (data) {
typingStuff();
}
else {
notTypingStuff();
}
});
}
但是,當用戶停止在3秒內輸入做notTypingStuff()
也應該知道..
它會是這樣的:http://jsfiddle.net/francescov/FYkYE/在Angular 2? – Stuart2041
如何使用ngModel及其方法ng-dirty和ng-pristine?你可以在Angular的文檔中閱讀它:https://angular.io/guide/forms#track-control-state-and-validity-with-ngmodel – Haseoh
@Haseoh看起來不錯,你能用一個例子發表一個答案嗎? :) – Stuart2041