Angular2 FormGroup - How to determine which control triggered valuechanges

Viewed 2889

I want to uncheck a checkbox any time there is a change in the form. The checkbox is part of that form so I want to only uncheck it when value change comes from any other control aside from that checkbox.

I am subscribing to a FormGroup's value changes like so

import { FormBuilder, FormGroup } from '@angular/forms';
export class App {
    form : FormGroup;
    constructor(private formBuilder: FormBuilder) {
        this.form = formBuilder.group({
            firstName: 'Thomas',
            lastName: 'Mann',
            ... many more fields ...
            checkbox: true
        })

        this.form.valueChanges.subscribe(data => {
            //if valueChange was not triggered by change in the checkbox
            this.form.get('checkbox').setValue(false);
        })
    }
}

I could subscribe to valueChanges in every other control individually, but would like to avoid doing so

1 Answers
Related