Angular react 形式未定义值

标签 angular angular-reactive-forms

我正在尝试显示在文本框中输入的值,但出现“错误类型错误:无法读取未定义的属性‘值’”错误。

这是我在组件中注册的 formControls:

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>

最佳答案

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

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

关于 Angular react 形式未定义值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53345677/

相关文章:

javascript - 使用 Angular 4 中的 ReactiveForms 使两个 FormControl 输入元素保持同步

angular - 如何使用 Angular5 检查表单控件的变化

angular - 如何解决错误 TS2304 : Cannot find name 'unknown' in a Angular/dotnet project build using the Okta SDK for angular

html - 如何处理 Angular 中自定义输入组件的输入事件?

Angular 表单 - 如何绑定(bind) formControlName 以在 formArrayName 中选择以使用对象

Angular6:自定义组件上的输入绑定(bind)未正确更新

angular - ngrx:提供的参数与调用目标的任何签名都不匹配

javascript - 如何使用多嵌套 json 值修补响应式(Reactive)表单

angular - 使用表单数组中另一个控件的值的最大验证器值

javascript - 手动设置 react 表单内表单数组元素的值