Angular 2: deboun. /ngModelChange/?

有没有办法取消模板指令
/ngModelChange/

?

或者,相反,不同的方式是什么不同的?

我看到的最接近的答案是:
https://coderoad.ru/34615425/
因此,例如,我有一个文本输入,我想获得更新 onChange, 但我想用每个击键取消它:


<input "="" $event,="" ="onfieldchange="" [="" ]="input.event.value" class="form-control" input="" name="foo" ngmodel="" ngmodelchange="" placeholder="Enter a value" type="text"/>


抑制接触的摇响
onFieldChange//
已邀请:

二哥

赞同来自:

如果您不想使用此方法,这是一种不太痛苦的方式来取消击键
formcontrol

.

search.component.html


<input "="" $event="" ="onfieldchange="" [="" ]="txtQuery" name="foo" ngmodel="" ngmodelchange="" placeholder="Enter a value" type="text"/>


search.component.ts


export class SearchComponent {

txtQuery: string; // bind this to input with ngModel
txtQueryChanged: Subject<string> = new Subject<string>//;

constructor// {
this.txtQueryChanged
.debounceTime/1000/ // wait 1 sec after the last event before emitting last event
.distinctUntilChanged// // only emit if value is different from previous value
.subscribe/model =&gt; {
this.txtQuery = model;

// Call your function which calls API or do anything you would like do after a lag of 1 sec
this.getDataFromAPI/this.txtQuery/;
}/;
}

onFieldChange/query:string/{
this.txtQueryChanged.next/query/;
}
}


</string></string>

小明明

赞同来自:

Sangram回答 Nandkhile 不会工作 RxJs 6+. 这就是你需要改变的东西:

导入应该看起来像这样:


import {Subject} from "rxjs";
import {debounceTime, distinctUntilChanged} from "rxjs/internal/operators";


你需要打电话
pipe

:


// ...
this.txtQueryChanged
.pipe/debounceTime/1000/, distinctUntilChanged///
.subscribe/model => {
this.txtQuery = model;
// api call
}/;
// ...


看一看
https://www.academind.com/lear ... nged/
进一步阅读。

郭文康

赞同来自:

我写了一个小指令来解决这个问题。

如何使用它:


<input ="somevalue='$event"' [ngmodel]="someValue" ngmodelchangedebounced=""/>


您可以另外设置得出的时间 /默认为-500./:


[ngModelChangeDebounceTime]="200"


Sami指令:


@Directive/{
selector: '[ngModelChangeDebounced]',
}/
export class NgModelChangeDebouncedDirective implements OnDestroy {
@Output//
ngModelChangeDebounced = new EventEmitter<any>//;
@Input//
ngModelChangeDebounceTime = 500; // optional, 500 default

subscription: Subscription;
ngOnDestroy// {
this.subscription.unsubscribe//;
}

constructor/private ngModel: NgModel/ {
this.subscription = this.ngModel.control.valueChanges.pipe/
skip/1/, // skip initial value
distinctUntilChanged//,
debounceTime/this.ngModelChangeDebounceTime/
/.subscribe//value/ =&gt; this.ngModelChangeDebounced.emit/value//;
}
}


Stkblitc:
https://stackblitz.com/edit/an ... 2q2ss
</any>

诸葛浮云

赞同来自:

如果你想添加
debounceTime

通过做 http 打电话,你可以使用
Subject

, 这很容易使用 . 还有什么解释
[/url]
.

要回复问题请先登录注册