Tuesday, 1 September 2020

FormGroup getControls, getFormArray, typescript getters, formGroup Validation

 getControls:

this.formGroup.get('name of you control')
// Get value
this.formGroup.get('name of you control').value
// Valuechanged
this.formGroup.get('name of you control').valueChanges.subscribe(changed=>{})
getControls:
 this.formGroup.controls.<yourControl>
// Get Form Array
(this.formGroup.controls.<yourControl> as FormArray).controls; 
// Getters for formControl
    get test(): AbstractControl {
        return this.formGroupFilter.get('testControl');
    }
// FormGroup Validation
   this.clicked$
            .pipe(
                tap(
                    (next) => {
   

                        //Tap return goes to MAP
                        if (this.formGroup.valid) {
                            return;
                        }

                        //
                        Object.keys(this.formGroup.controls).forEach(field => {
                            const control = this.formGroup.get(field);
                            control.markAsTouched({ onlySelf: true });
                            control.updateValueAndValidity();
                        });
                    },
                ),
                switchMap(
                    (next) => {

                        return this.formGroup.statusChanges
                            .pipe(
                                // Begin with a startWith to ensure there's no
                                // hanging emission, in the case the form is
                                // valid at the time of submission.
                                startWith(this.formGroup.status),
                                // Filtering by PENDING waits for this status to
                                // change
                                filter(status => status !== 'PENDING'),
                                // take(1) makes sure the stream is completed on
                                // the first emission after pending: VALID or
                                // INVALID.
                                take(1),
                            );
                    },
                ),
                tap(
                    (status) => {
                      
                           // Can do additional LOG here
                          // Validator already shows alert
                    },
                ),
                filter(status => status === 'VALID'),
            )
            .subscribe(
                (next) => {
   
                },
                (error: HttpErrorResponse) => {
                  
                },
            );

No comments:

Post a Comment