Sunday, August 6, 2017

just a few more notes on Angular 4 reactive forms stolen out of an online training

  1. *ngIf="v.get('emailAddress').valid" is an example of using the get operator in reactive forms (the form is v in this case) to fish out field details.
  2. *ngIf="v.get('mySubGroup.emailAddress').valid" shows off the same thing while drilling into a group.
  3. You make your own custom validator that uses a method. To do so, eplace Validators.required at your FormControl with something vaguely like [Validators.required, this.myGimmick.bind(this)] while the myGimmick method elsewhere in your component (with, yes, bonus, this scoped correctly) should have a signature like this:
    myGimmick(formControl: formControl): {[key:string]:boolean} {
    Herein, {[key:string]:boolean} is like a KeyValuePair of <string,boolean> in C# while formControl.value will give you the value you want to operate on in your method. Your method may do whatever you like. You may pull the value out of a different control with the .get operator for a cross-comparison of two dates for example if birthday should come before death date or something like that. Do what you want. It's on you. When you are ready to pass a validation, either return nothing or return null. When you want to fail a validation return something like so:
    return {'arbitraryName':true};
  4. *ngIf="v.get('birthday').errors['arbitraryName']" is the way to check if there is an error and that is kinda frustrating and clunky. You need a separate break out for *ngIf="v.get('birthday').errors['required']" too which sucks.
  5. asyncGimmick(formControl: formControl): Promise<any> | Observable<any> {
       const promise = new Promise<any>((resolve, reject) => {
          setTimeout(() => {
             if (formControl.value === this.doomsDay) {
                resolve({'asyncName':true});
             } else {
                resolve(null);
             }
          }, 1500);
       });
       return promise;
    }

    ...is an example of an asynchronous validation. It gets wired up a bit differently at FormControls like so:
    'birthDay': new FormControl(null, [Validators.required, this.myGimmick.bind(this)],
          this.asyncGimmick.bind(this))

    ...and while this will decorate a control with ng-valid and ng-invalid CSS classes there will be a window during which an ng-pending class is present instead.
  6. this.v.valueChanges.subscribe{
       (value) => console.log(value)
    );
    is an example of reacting whenever a change occurs. This also exists at the individual control level and at the individual control level and form level both there is also a statusChanges too.
  7. this.v.patchValue and this.v.setValue are legit in the reactive forms stuff and there is also a v.reset() as well. These are just like their template-driven forms counterparts.
  8. <a *ngFor="let foo of foos; let i = index" (click)="onWhatever(i)"> is an example of passing the current line item's identity to the TypeScript side of things when doing the FormArray thing.

No comments:

Post a Comment