Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skip programmatic changes in valueChanges of Angular 2 control

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>;

How can I filter this down to give me only the changes from the UI and not the programmatic ones?

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.

like image 850
EagleBeak Avatar asked Jan 31 '17 10:01

EagleBeak


1 Answers

You can use

control.setValue(123, {emitEvent: false})

See also

  • 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
like image 196
Günter Zöchbauer Avatar answered Sep 27 '22 23:09

Günter Zöchbauer