簡體   English   中英

角度反應形成未定義的值

[英]angular reactive forms undefined value

我試圖顯示在文本框中輸入的值,但我得到一個“錯誤的TypeError:無法讀取未定義的屬性'值'錯誤”。

這是我在我的組件中注冊的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>

您需要使用反應形式提供的方法:

siteResetForm.get('attuid').value

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM