Using Form Data in Angular Template-driven forms


<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()" #myForm="ngForm">
        <div id="user-data"
          <div class="form-group">
            <label for="username">Username</label>
            <input type="text" id="username" class="form-control"
                   ngModel name="username"
          <button class="btn btn-default" type="button"
                  (click)="suggestUserName()">Set All Values

          <button class="btn btn-default" type="button"
                  (click)="overrideUseName()">Override Part of Values
          <div class="form-group">
            <label for="email">Mail</label>
            <input type="email" id="email" class="form-control"
                   ngModel name="email"
                   required email
            <span class="help-block" *ngIf="!myEmail.valid && myEmail.touched">Please enter a valid email!</span>
        <p *ngIf="!myUserData.valid && myUserData.touched">User Data is invalid!</p>
        <div class="form-group">
          <label for="secret">Secret Questions</label>
          <select id="secret" class="form-control"
            <option value="pet">Your first Pet?</option>
            <option value="teacher">Your first teacher?</option>
          <div class="form-group">
            <textarea name="questionAnswer" rows="3" class="form-control"
        <div>Your replay: {{answer}}</div>
        <div class="radio" *ngFor="let gender of genders">
            <input type="radio" required name="gender" ngModel [value]="gender">
        <button class="btn btn-primary" type="submit"
  <div class="row" *ngIf="submitted">
    <div class="col-xs-12">
      <h3>Your Data</h3>
      <p>Username: {{user.username}}</p>
      <p>Mail: {{}}</p>
      <p>Secret Question: {{user.question}}</p>
      <p>Answer: {{user.answer}}</p>
      <p>Gender: {{user.gender}}</p>


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

  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
export class AppComponent {

  @ViewChild('myForm') signupForm: NgForm;
  answer: string;
  genders = ['male', 'female'];
  user = {
    username: '',
    email: '',
    question: '',
    answer: '',
    gender: ''
  submitted = false;

   * Set values all together
  suggestUserName() {
    const suggestedName = 'Superuser';
      myUserData: {
        username: suggestedName,
        email: '[email protected]'
      secret: 'pet',
      questionAnswer: 'Hello Wolrd',
      gender: 'male'

   * Set part of values
  overrideUseName() {
    const suggestedName = 'Superuser';
      myUserData: {
        username: suggestedName

  onSubmit() {
    this.user.username = this.signupForm.value.myUserData.username; =;
    this.user.question = this.signupForm.value.secret;
    this.user.answer = this.signupForm.value.questionAnswer;
    this.user.gender = this.signupForm.value.gender;
    this.submitted = true;