0

As the title says, I've started learning Angular using a video course and I don't really seem to understand the behavior of some code.

For an app component, I have this code: html

<div class="container">
  <div class="row">
    <div class="col-xs-12 col-sm-10 col-md-8 col-sm-offset-1 col-md-offset-2">
      <form (ngSubmit)="onSubmit(f)" #f="ngForm">
        <div id="user-data" ngModelGroup="userData" #userData="ngModelGroup">
          <div class="form-group">
            <label for="username">Username</label>
            <input type="text" id="username" class="form-control" ngModel name="username" required> 
          </div>
          <button class="btn btn-default" type="button" (click)="suggestUserName()">Suggest an Username</button>
          <div class="form-group">
            <label for="email">Mail</label>
            <input type="email" id="email" class="form-control" ngModel name="email" email required>
          </div>
        </div>
        <div class="form-group">
          <label for="secret">Secret Questions</label>
          <select id="secret" class="form-control" ngModel name="secret">
            <option value="pet">Your first Pet?</option>
            <option value="teacher">Your first teacher?</option>
          </select>
        </div>
        <button class="btn btn-primary" type="submit">Submit</button>
      </form>
    </div>
  </div>
</div>

ts

import { Component, ViewChild } from '@angular/core';
import { NgForm } from '@angular/forms';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  @ViewChild('f') signupForm: NgForm;

  suggestUserName() {
    const suggestedName = 'Superuser';
    this.signupForm.form.patchValue({
      userData: {
        username: suggestedName
      }
    });
  }

  onSubmit(form: NgForm) {
    console.log(form);
  }
}

And everything is working fine. The line that I don't really understand is this one:

<div id="user-data" ngModelGroup="userData" #userData="ngModelGroup">

I've made a typo before, instead of userData I wrote userdata, like this:

<div id="user-data" ngModelGroup="userdata" #userData="ngModelGroup">

and I didn't get the expected behavior of my app anymore (even though I thought I would)

In this piece of code:

this.signupForm.form.patchValue({
      userData: {
        username: suggestedName
      }
    });

what does userData refer to? The userData from here:

ngModelGroup="userData"

or this one

#userData="ngModelGroup"

Thanks.

C.Champagne
  • 5,381
  • 2
  • 23
  • 35
  • Doesn't the fact that changing to `ngModelGroup="userdata"` changed the behaviour tell you that's the one that's being referred to? – jonrsharpe Nov 29 '20 at 11:54
  • Protip: always include the language tag for the language you are asking a question about (I added Javascript for you) otherwise you won't get automatic syntax highlighting. – Jared Smith Nov 29 '20 at 11:57
  • @jonrsharpe I thought so. But then, why do I need #userData="ngModelGroup"? –  Nov 29 '20 at 12:15

0 Answers0