我试图显示在文本框中输入的值,但出现“错误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>
答案 0 :(得分:2)
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>
答案 1 :(得分:1)
You need to use the methods provided in reactive forms:
siteResetForm.get('attuid').value