rf guy
rf guy

Reputation: 439

angular reactive forms undefined value

I am trying to display the value as it is being entered in a text box but I am getting a "ERROR TypeError: Cannot read property 'value' of undefined" error.

Here is my my registered formControls in my component:

import { Component } from '@angular/core';
import { FormGroup, FormControl, Validators } from '@angular/forms';


@Component({
  selector: 'app-site-reset',
  templateUrl: './site-reset.component.html',
  styleUrls: ['./site-reset.component.scss']
})
export class SiteResetComponent {
  siteResetForm = new FormGroup({
    attuid: new FormControl(''),
    contactNumber: new FormControl(''),
    vendor: new FormControl(''),
    severity: new FormControl(''),
    siteNum: new FormControl(''),
    reasonSelect: new FormControl(''),
    other: new FormControl(''),
    pilot: new FormControl(''),
    alarms: new FormControl(''),
    adjacent: new FormControl(''),
    reset: new FormControl(''),
    anr: new FormControl(''),
    tickets: new FormControl(''),
    other_action: new FormControl(''),
    date: new FormControl(''),
  });

and the beginning of my template where I am trying to print the data entered

<div class="container-fluid">
  <h2>Site Reset (basic) form</h2>

  <div class="row">

    <div class="col-5">
      <div class="p-3 mb-2 bg-primary text-white">
        <form [formGroup]="siteResetForm" (ngSubmit)="onSubmit()">

          <div class="form-group">
            <label>ATTUID:</label>
            <input type="text" class="form-control" placeholder="ATTUID" formControlName="attuid">
          </div>

          <p>
            Value: {{ attuid.value }}
          </p>

Upvotes: 5

Views: 7923

Answers (2)

JuanF
JuanF

Reputation: 876

You need to use the methods provided in reactive forms:

siteResetForm.get('attuid').value

Upvotes: 1

John Gallego
John Gallego

Reputation: 681

attuid is not exposed to the template directly. You need to access it by referencing the form group which is exposed to the template.

<p>Value: {{ siteResetForm.controls.attuid.value }}</p>

Upvotes: 2

Related Questions