1
\$\begingroup\$

I'm building an autosave for a data driven form on Angular.

Here is our code snippet:

public ngOnInit(): void {
    this.form = this.formBuilder.group({
        name: ['', Validators.required],
        enabled: [false],
    });

    this.form.valueChanges.pipe(
        debounceTime(500),
        withLatestFrom(this.form.statusChanges.pipe(filter(status => status === 'VALID')))
    ).subscribe((value) => console.log('We could autosave!', value[0], 'status', value[1]));

}

It works but I wonder if it's optimal to use the statusChanges.

Indeed we could use the valid property instead:

this.form.valueChanges.pipe(
      debounceTime(500)
).subscribe((value) => {
    if (this.form.valid) {
        console.log('We could autosave!', value);
    }
});

What would be the most reliable/optimal method?

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.