Chris Straw
SHARE:

Angular Copy/Paste – Subscribe to the valueChanges for more than 1 field

Just a code snippet for copy/paste to subscribe to the valueChanges for more than 1 field.

    const sub = merge(
      fg.get('field1')!.valueChanges.pipe(map((value) => ({ field1: value, field2: fg.get('field2')?.value, field3: fg.get('field3')?.value }))),
      fg.get('field2')!.valueChanges.pipe(map((value) => ({ field1: fg.get('field1')?.value, field2: value,  field3: fg.get('field3')?.value }))),
      fg.get('field3')!.valueChanges.pipe(map((value) => ({ field1: fg.get('field1')?.value, field2: fg.get('field2')?.value, field3: value })))
    ).subscribe(data => {
      const hasValue = OltUtility.toBoolean(data?.field2) === true || OltUtility.hasValue(data?.field1) || OltUtility.toInt(data?.field3, 0) > 1;
      if (hasValue) {
        fg.get('select')?.setValue(true);
      }
    });