跳过 Angular 2 控件的 valueChanges 中的编程更改 [英] Skip programmatic changes in valueChanges of Angular 2 control
问题描述
我订阅了 Angular 2 (2.2.1) 控件的 valueChanges
可观察对象.它在@angular\forms\src\model.d.ts 中的 AbstractControl
中定义,并且它的文档字符串指出它将从 UI 和程序化的改变 yiald:
I'm subscribing to the valueChanges
observable of an Angular 2 (2.2.1) control. It's defined in AbstractControl
in @angular\forms\src\model.d.ts and it's doc string states that it will yiald changes from the UI as well as programmatic ones:
/**
* Emits an event every time the value of the control changes, in
* the UI or programmatically.
*/
valueChanges: Observable<any>;
我如何过滤它以仅提供 UI 的更改而不是程序化的更改?
我认为布尔属性(原始的、脏的、触摸的等)对我无济于事,因为即使在控件被标记为脏之后 - 指示 UI 的更改,我想捕获 - 可能有是进一步的程序更改,我想忽略.
I think that the boolean props (pristine, dirty, touched, etc.) won't help me, because even after a control is marked dirty - indicating a change from the UI, which I would like to capture - there might be further programmatic changes, which I would like to ignore.
推荐答案
你可以使用
control.setValue(123, {emitEvent: false})
另见
- https://angular.io/docs/ts/latest/api/forms/index/AbstractControl-class.html#!#setValue-anchor
- https://github.com/angular/angular/blob/e9f307f9488e44879dc027e5f4436fb6bc046fa4/modules/%40angular/forms/src/model.ts#L669
这篇关于跳过 Angular 2 控件的 valueChanges 中的编程更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!