Angular响应式表单中的未定义值

5

我想在文本框中输入时实时显示值,但是我遇到了 "ERROR TypeError: Cannot read property 'value' of undefined" 的错误。

以下是我的组件中注册的表单控件:

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(''),
  });

我正在尝试打印输入的数据,这是我的模板的开头

<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>
2个回答

2

attuid并不直接暴露给模板。您需要通过引用向模板公开的表单组来访问它。

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

谢谢!那个方法有效。你介意给我的问题点个赞吗?我想要提高声望。 - rf guy

1

您需要使用响应式表单中提供的方法:

siteResetForm.get('attuid').value

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接