Angular и debounce
В AngularJS я могу отменить модель с помощью опций ng-model.
ng-model-options="{ debounce: 1000 }"
Как я могу отменить модель в Angular? Я попытался найти debounce в документах, но ничего не нашел.
https://angular.io/search/#stq=debounce&stp=1
Решением будет написать мою собственную функцию 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);
И мой html
<input type=text [value]="firstName" #first (keyup)="firstNameChanged($event, first)">
Но я ищу встроенную функцию, есть ли в Angular?
Ответы
Ответ 1
Обновлено для RC.5
С помощью Angular 2 мы можем отказаться от использования оператора RxJS debounceTime()
для значения контроля valueChanges
Наблюдаемые valueChanges
:
import {Component} from '@angular/core';
import {FormControl} from '@angular/forms';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/throttleTime';
import 'rxjs/add/observable/fromEvent';
@Component({
selector: 'my-app',
template: '<input type=text [value]="firstName" [formControl]="firstNameControl">
<br>{{firstName}}'
})
export class AppComponent {
firstName = 'Name';
firstNameControl = new FormControl();
formCtrlSub: Subscription;
resizeSub: Subscription;
ngOnInit() {
// debounce keystroke events
this.formCtrlSub = this.firstNameControl.valueChanges
.debounceTime(1000)
.subscribe(newValue => this.firstName = newValue);
// throttle resize events
this.resizeSub = Observable.fromEvent(window, 'resize')
.throttleTime(200)
.subscribe(e => {
console.log('resize event', e);
this.firstName += '*'; // change something to show it worked
});
}
ngDoCheck() { console.log('change detection'); }
ngOnDestroy() {
this.formCtrlSub.unsubscribe();
this.resizeSub .unsubscribe();
}
}
Plunker
В приведенном выше коде также приведен пример того, как изменять размер окна для изменения размера окна, как задал @albanx в комментарии ниже.
Хотя приведенный выше код, вероятно, является Угловым способом его выполнения, он неэффективен. Каждое нажатие клавиши и каждое изменение размера, даже если они дебютируют и дросселируются, приводят к запуску изменения. Другими словами, debouncing и throttling не влияют на то, как часто происходит изменение обнаружения. (Я нашел комментарий от GitHub от Tobias Bosch, который подтверждает это.) Это можно увидеть, когда вы запускаете плункер, и вы видите, сколько раз ngDoCheck()
при вводе в поле ввода или изменении размера окна. (Используйте синюю кнопку "x", чтобы запустить плункер в отдельном окне, чтобы увидеть события изменения размера.)
Более эффективная методика заключается в создании RxJS. Соблюдайте сами события, вне угловой "зоны". Таким образом, обнаружение изменений не вызывается каждый раз, когда срабатывает событие. Затем в методах обратного вызова подписки вручную инициируйте обнаружение изменений - т.е. Вы контролируете, когда вызывается обнаружение изменений:
import {Component, NgZone, ChangeDetectorRef, ApplicationRef,
ViewChild, ElementRef} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/throttleTime';
import 'rxjs/add/observable/fromEvent';
@Component({
selector: 'my-app',
template: '<input #input type=text [value]="firstName">
<br>{{firstName}}'
})
export class AppComponent {
firstName = 'Name';
keyupSub: Subscription;
resizeSub: Subscription;
@ViewChild('input') inputElRef: ElementRef;
constructor(private ngzone: NgZone, private cdref: ChangeDetectorRef,
private appref: ApplicationRef) {}
ngAfterViewInit() {
this.ngzone.runOutsideAngular( () => {
this.keyupSub = Observable.fromEvent(this.inputElRef.nativeElement, 'keyup')
.debounceTime(1000)
.subscribe(keyboardEvent => {
this.firstName = keyboardEvent.target.value;
this.cdref.detectChanges();
});
this.resizeSub = Observable.fromEvent(window, 'resize')
.throttleTime(200)
.subscribe(e => {
console.log('resize event', e);
this.firstName += '*'; // change something to show it worked
this.cdref.detectChanges();
});
});
}
ngDoCheck() { console.log('cd'); }
ngOnDestroy() {
this.keyupSub .unsubscribe();
this.resizeSub.unsubscribe();
}
}
Plunker
Я использую ngAfterViewInit()
вместо ngOnInit()
чтобы определить значение параметра inputElRef
.
detectChanges()
будет запускать обнаружение изменений этого компонента и его дочерних элементов. Если вы предпочтете запустить обнаружение изменений из корневого компонента (т.е. Запустите полную проверку обнаружения изменений ApplicationRef.tick()
вместо этого используйте ApplicationRef.tick()
. (Я делаю вызов ApplicationRef.tick()
в комментариях в плункере.) Обратите внимание, что вызов tick()
вызовет ngDoCheck()
.
Ответ 2
Если вы не хотите иметь дело с @angular/forms
, вы можете просто использовать RxJS Subject
с привязками изменений.
view.component.html
<input [ngModel]='model' (ngModelChange)='changed($event)' />
view.component.ts
import { Subject } from 'rxjs/Subject';
import { Component } from '@angular/core';
import 'rxjs/add/operator/debounceTime';
export class ViewComponent {
model: string;
modelChanged: Subject<string> = new Subject<string>();
constructor() {
this.modelChanged
.debounceTime(300) // wait 300ms after the last event before emitting last event
.distinctUntilChanged() // only emit if value is different from previous value
.subscribe(model => this.model = model);
}
changed(text: string) {
this.modelChanged.next(text);
}
}
Это вызывает обнаружение изменений. Для способа, который не вызывает обнаружение изменений, проверьте Отметить ответ.
Обновить
.pipe(debounceTime(300), distinctUntilChanged())
необходим для rxjs 6.
Пример:
constructor() {
this.modelChanged.pipe(
debounceTime(300),
distinctUntilChanged())
.subscribe(model => this.model = model);
}
Ответ 3
Он может быть реализован в качестве Директивы
import { Directive, Input, Output, EventEmitter, OnInit, OnDestroy } from '@angular/core';
import { NgControl } from '@angular/forms';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/distinctUntilChanged';
import { Subscription } from 'rxjs';
@Directive({
selector: '[ngModel][onDebounce]',
})
export class DebounceDirective implements OnInit, OnDestroy {
@Output()
public onDebounce = new EventEmitter<any>();
@Input('debounce')
public debounceTime: number = 300;
private isFirstChange: boolean = true;
private subscription: Subscription;
constructor(public model: NgControl) {
}
ngOnInit() {
this.subscription =
this.model.valueChanges
.debounceTime(this.debounceTime)
.distinctUntilChanged()
.subscribe(modelValue => {
if (this.isFirstChange) {
this.isFirstChange = false;
} else {
this.onDebounce.emit(modelValue);
}
});
}
ngOnDestroy() {
this.subscription.unsubscribe();
}
}
использовать его как
<input [(ngModel)]="value" (onDebounce)="doSomethingWhenModelIsChanged($event)">
образец компонента
import { Component } from "@angular/core";
@Component({
selector: 'app-sample',
template: '
<input[(ngModel)]="value" (onDebounce)="doSomethingWhenModelIsChanged($event)">
<input[(ngModel)]="value" (onDebounce)="asyncDoSomethingWhenModelIsChanged($event)">
'
})
export class SampleComponent {
value: string;
doSomethingWhenModelIsChanged(value: string): void {
console.log({ value });
}
async asyncDoSomethingWhenModelIsChanged(value: string): Promise<void> {
return new Promise<void>(resolve => {
setTimeout(() => {
console.log('async', { value });
resolve();
}, 1000);
});
}
}
Ответ 4
Недоступно, как в угловом1, но вы можете легко играть с наблюдаемыми NgFormControl и RxJS:
<input type="text" [ngFormControl]="term"/>
this.items = this.term.valueChanges
.debounceTime(400)
.distinctUntilChanged()
.switchMap(term => this.wikipediaService.search(term));
В этом блоге ясно сказано:
http://blog.thoughtram.io/angular/2016/01/06/taking-advantage-of-observables-in-angular2.html
Здесь это для автозаполнения, но он работает со всеми сценариями.
Ответ 5
Поскольку тема старая, большинство ответов не работают на Angular 6/7/8.
Итак, вот краткое и простое решение для Angular 6+ с RxJS.
Сначала импортируйте необходимые данные:
import { Component, OnInit, OnDestroy } from '@angular/core';
import { Subject, Subscription } from 'rxjs';
import { debounceTime, distinctUntilChanged } from 'rxjs/operators';
Инициализировать на ngOnInit
:
export class MyComponent implements OnInit, OnDestroy {
notesText: string;
private notesModelChanged: Subject<string> = new Subject<string>();
private notesModelChangeSubscription: Subscription
constructor() { }
ngOnInit() {
this.notesModelChangeSubscription = this.notesModelChanged
.pipe(
debounceTime(2000),
distinctUntilChanged()
)
.subscribe(newText => {
this.notesText = newText;
console.log(newText);
});
}
ngOnDestroy() {
this.notesModelChangeSubscription.unsubscribe();
}
}
Используйте этот способ:
<input [ngModel]='notesText' (ngModelChange)='notesModelChanged.next($event)' />
P.S.: Для более сложных и эффективных решений вы все равно можете проверить другие ответы.
Ответ 6
Вы можете создать RxJS (v.6) Observable, который делает все, что вам нравится.
view.component.html
<input type="text" (input)="onSearchChange($event.target.value)" />
view.component.ts
import { Observable } from 'rxjs';
import { debounceTime, distinctUntilChanged } from 'rxjs/operators';
export class ViewComponent {
searchChangeObserver;
onSearchChange(searchValue: string) {
if (!this.searchChangeObserver) {
Observable.create(observer => {
this.searchChangeObserver = observer;
}).pipe(debounceTime(300)) // wait 300ms after the last event before emitting last event
.pipe(distinctUntilChanged()) // only emit if value is different from previous value
.subscribe(console.log);
}
this.searchChangeObserver.next(searchValue);
}
}
Ответ 7
Для тех, кто использует lodash, чрезвычайно легко отменить любую функцию:
changed = _.debounce(function() {
console.log("name changed!");
}, 400);
затем просто добавьте что-то подобное в ваш шаблон:
<(input)="changed($event.target.value)" />
Ответ 8
Я решил это, написав декоратор debounce. Описанную проблему можно решить, применив @debounceAccessor к аксессуру набора свойств.
Я также предоставил дополнительный декор debounce для методов, которые могут быть полезны для других случаев.
Это очень упрощает отладку свойства или метода. Параметр - это количество миллисекунд, которое должно длиться debounce, 100 мс в приведенном ниже примере.
@debounceAccessor(100)
set myProperty(value) {
this._myProperty = value;
}
@debounceMethod(100)
myMethod (a, b, c) {
let d = a + b + c;
return d;
}
И вот код для декораторов:
function debounceMethod(ms: number, applyAfterDebounceDelay = false) {
let timeoutId;
return function (target: Object, propName: string, descriptor: TypedPropertyDescriptor<any>) {
let originalMethod = descriptor.value;
descriptor.value = function (...args: any[]) {
if (timeoutId) return;
timeoutId = window.setTimeout(() => {
if (applyAfterDebounceDelay) {
originalMethod.apply(this, args);
}
timeoutId = null;
}, ms);
if (!applyAfterDebounceDelay) {
return originalMethod.apply(this, args);
}
}
}
}
function debounceAccessor (ms: number) {
let timeoutId;
return function (target: Object, propName: string, descriptor: TypedPropertyDescriptor<any>) {
let originalSetter = descriptor.set;
descriptor.set = function (...args: any[]) {
if (timeoutId) return;
timeoutId = window.setTimeout(() => {
timeoutId = null;
}, ms);
return originalSetter.apply(this, args);
}
}
}
Я добавил дополнительный параметр для декоратора метода, который позволяет запускать метод ПОСЛЕ задержки дебюта. Я сделал это, чтобы я мог, например, использовать его в сочетании с событиями mouseover или resize, где я хотел, чтобы захват происходил в конце потока событий. Однако в этом случае метод не вернет значение.
Ответ 9
Мы можем создать директиву [debounce], которая перезаписывает функцию ngModel по умолчанию viewToModelUpdate с пустым.
Код директивы
@Directive({ selector: '[debounce]' })
export class MyDebounce implements OnInit {
@Input() delay: number = 300;
constructor(private elementRef: ElementRef, private model: NgModel) {
}
ngOnInit(): void {
const eventStream = Observable.fromEvent(this.elementRef.nativeElement, 'keyup')
.map(() => {
return this.model.value;
})
.debounceTime(this.delay);
this.model.viewToModelUpdate = () => {};
eventStream.subscribe(input => {
this.model.viewModel = input;
this.model.update.emit(input);
});
}
}
Как использовать его
<div class="ui input">
<input debounce [delay]=500 [(ngModel)]="myData" type="text">
</div>
Ответ 10
Простым решением было бы создать директиву, которую вы можете применить к любому элементу управления.
import { Directive, ElementRef, Input, Renderer, HostListener, Output, EventEmitter } from '@angular/core';
import { NgControl } from '@angular/forms';
@Directive({
selector: '[ngModel][debounce]',
})
export class Debounce
{
@Output() public onDebounce = new EventEmitter<any>();
@Input('debounce') public debounceTime: number = 500;
private modelValue = null;
constructor(public model: NgControl, el: ElementRef, renderer: Renderer){
}
ngOnInit(){
this.modelValue = this.model.value;
if (!this.modelValue){
var firstChangeSubs = this.model.valueChanges.subscribe(v =>{
this.modelValue = v;
firstChangeSubs.unsubscribe()
});
}
this.model.valueChanges
.debounceTime(this.debounceTime)
.distinctUntilChanged()
.subscribe(mv => {
if (this.modelValue != mv){
this.modelValue = mv;
this.onDebounce.emit(mv);
}
});
}
}
использование будет
<textarea [ngModel]="somevalue"
[debounce]="2000"
(onDebounce)="somevalue = $event"
rows="3">
</textarea>
Ответ 11
HTML файл:
<input [ngModel]="filterValue"
(ngModelChange)="filterValue = $event ; search($event)"
placeholder="Search..."/>
TS файл:
timer = null;
time = 250;
search(searchStr : string) : void {
clearTimeout(this.timer);
this.timer = setTimeout(()=>{
console.log(searchStr);
}, time)
}
Ответ 12
Отправленные часы, надеюсь, я смогу сэкономить еще какое-то время. Для меня более понятный и понятный для меня подход к использованию debounce
на элементе управления. Он построен на решении angular.io docs для автозаполнения, но с возможностью перехвата вызовов без необходимости связывать данные с DOM.
Plunker
Случайный сценарий для этого может быть проверкой имени пользователя после его ввода, чтобы узнать, уже ли кто-то его принял, а затем предупредил пользователя.
Примечание: не забывайте, что (blur)="function(something.value)
может иметь больше смысла для вас в зависимости от ваших потребностей.
Ответ 13
Решение с подписчиком инициализации непосредственно в функции события:
import {Subject} from 'rxjs';
import {debounceTime, distinctUntilChanged} from 'rxjs/operators';
class MyAppComponent {
searchTermChanged: Subject<string> = new Subject<string>();
constructor() {
}
onFind(event: any) {
if (this.searchTermChanged.observers.length === 0) {
this.searchTermChanged.pipe(debounceTime(1000), distinctUntilChanged())
.subscribe(term => {
// your code here
console.log(term);
});
}
this.searchTermChanged.next(event);
}
}
И html:
<input type="text" (input)="onFind($event.target.value)">
Ответ 14
DebounceTime в Angular 7 с RxJS v6
Ссылка на источник
Демо- ссылка
![enter image description here]()
В шаблоне HTML
<input type="text" #movieSearchInput class="form-control"
placeholder="Type any movie name" [(ngModel)]="searchTermModel" />
В компоненте
....
....
export class AppComponent implements OnInit {
@ViewChild('movieSearchInput') movieSearchInput: ElementRef;
apiResponse:any;
isSearching:boolean;
constructor(
private httpClient: HttpClient
) {
this.isSearching = false;
this.apiResponse = [];
}
ngOnInit() {
fromEvent(this.movieSearchInput.nativeElement, 'keyup').pipe(
// get value
map((event: any) => {
return event.target.value;
})
// if character length greater then 2
,filter(res => res.length > 2)
// Time in milliseconds between key events
,debounceTime(1000)
// If previous query is diffent from current
,distinctUntilChanged()
// subscription for response
).subscribe((text: string) => {
this.isSearching = true;
this.searchGetCall(text).subscribe((res)=>{
console.log('res',res);
this.isSearching = false;
this.apiResponse = res;
},(err)=>{
this.isSearching = false;
console.log('error',err);
});
});
}
searchGetCall(term: string) {
if (term === '') {
return of([]);
}
return this.httpClient.get('http://www.omdbapi.com/?s=' + term + '&apikey=' + APIKEY,{params: PARAMS.set('search', term)});
}
}
Ответ 15
Для реактивных форм и обработки в Angular v2 (последний) плюс v4 смотрите:
https://github.com/angular/angular/issues/6895#issuecomment-290892514
Надеюсь, скоро будет такая поддержка для этих вещей...
Ответ 16
Это лучшее решение, которое я нашел до сих пор. ngModel
на blur
и debounce
import { Directive, Input, Output, EventEmitter,ElementRef } from '@angular/core';
import { NgControl, NgModel } from '@angular/forms';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/distinctUntilChanged';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/fromEvent';
import 'rxjs/add/operator/map';
@Directive({
selector: '[ngModel][debounce]',
})
export class DebounceDirective {
@Output()
public onDebounce = new EventEmitter<any>();
@Input('debounce')
public debounceTime: number = 500;
private isFirstChange: boolean = true;
constructor(private elementRef: ElementRef, private model: NgModel) {
}
ngOnInit() {
const eventStream = Observable.fromEvent(this.elementRef.nativeElement, 'keyup')
.map(() => {
return this.model.value;
})
.debounceTime(this.debounceTime);
this.model.viewToModelUpdate = () => {};
eventStream.subscribe(input => {
this.model.viewModel = input;
this.model.update.emit(input);
});
}
}
как заимствован из fooobar.com/questions/39896/...
Затем в HTML:
<input [(ngModel)]="hero.name"
[debounce]="3000"
(blur)="hero.name = $event.target.value"
(ngModelChange)="onChange()"
placeholder="name">
При blur
модель явно обновляется с использованием простого javascript.
Пример здесь: https://stackblitz.com/edit/ng2-debounce-working
Ответ 17
@Component({
selector: 'input-debounce',
template: '<input type="text" class="form-control" [placeholder]="placeholder" [(ngModel)]="inputValue">'
})
export class InputDebounceComponent {
@Input() placeholder: string;
@Input() delay: number = 300;
@Output() value: EventEmitter = new EventEmitter();
public inputValue: string;
constructor(private elementRef: ElementRef) {
const eventStream = Observable.fromEvent(elementRef.nativeElement, 'keyup')
.map(() => this.inputValue)
.debounceTime(this.delay)
.distinctUntilChanged();
eventStream.subscribe(input => this.value.emit(input));
}
}
Ответ 18
Я надеюсь, что это поможет вам разобраться как с шаблонными, так и с реактивными формами в Angular 6.