Saturday, August 5, 2017

more template-driven Angular 4 form notes from a series of online trainings

  1. Wrap two controls in div and put something like...
    ngModelGroup="myThing" #myThing="mgModelGroup"
    ...inline in the tag to group controls allowing for one to do stuff like:
    *ngIf="!myThing.valid && myThing.touched"
  2. For radio buttons, just have one button inside of an *ngFor driven from an array of options. Put ngModel inside the radio button's tag.
  3. this.myForm.setValue({}) is going to set everything on a form with a default value. You do not use it as shown here with an empty object, but instead the object needs to be representative of the shape of the form with objects nested in groups nested in subobjects, etc. this.myForm.form.patchValue({}) in contrast only sets values for the fields specified and not everything.
  4. let x = this.myForm.value.emailAddress; is an example of fishing stuff back out of a form.
  5. this.myForm.reset(); is going to clear out the values and also reset states like valid and touched to their defaults.

No comments:

Post a Comment