Angular FormGroup 中的 2/4 FormControl 无法在带有标签的表单上显示
Angular 2/4 FormControl in FormGroup not able to display on form with label
我不明白该怎么做
- console.log 特定值
- 显示在 HTML 页面的标签中
- 在输入文本中显示
这是我的组件 Typescript,带有新的 FormGroup,然后是新的 FormControls
this.trackerForm = new FormGroup({
session: new FormControl('', Validators.required),
date: new FormControl(new Date(), [
Validators.required
]),
contactType: new FormControl('', [
Validators.required
]),
customerType: new FormControl('', Validators.required),
firstName: new FormControl('', [Validators.required, Validators.minLength(80)]),
lastName: new FormControl('', [Validators.required, Validators.minLength(80)]),
phone: new FormControl('', [Validators.required, Validators.maxLength(10), Validators.minLength(10)]),
extension: new FormControl('', [Validators.maxLength(7)])
});
// this outputs the entire json
console.log(JSON.stringify(this.trackerForm.value));
//How do I ONLY console.log one of the values? date?
HTML 页 -
<form [formGroup]="trackerForm" (ngSubmit)="onSubmit(trackerForm.value)" novalidate>
<div>
<label class="form-control"><span>{{trackerForm.date.value}} </span></label>
</div>
您需要通过 trackerForm.controls
对象访问 formGroup
控件。
"date"
控件值 trackerForm.controls['date'].value
的示例。
<form [formGroup]="trackerForm" (ngSubmit)="onSubmit(trackerForm.value)" novalidate>
<div>
<label class="form-control"><span>{{trackerForm.get('date').value}} </span></label>
</div>
或
<label class="form-control"><span>{{trackerForm.controls['date'].value}} </span></label>
但第一个肯定更好,因为 'AOT' 不编译第二个。
但我会创建一个组件 getter
以使其更好:
get dateControl(){
return this.trackerForm.get('date');
}
然后:
<form [formGroup]="trackerForm" (ngSubmit)="onSubmit(trackerForm.value)" novalidate>
<div>
<label class="form-control"><span>{{dateControl.value}} </span></label>
</div>
我不明白该怎么做
- console.log 特定值
- 显示在 HTML 页面的标签中
- 在输入文本中显示
这是我的组件 Typescript,带有新的 FormGroup,然后是新的 FormControls
this.trackerForm = new FormGroup({
session: new FormControl('', Validators.required),
date: new FormControl(new Date(), [
Validators.required
]),
contactType: new FormControl('', [
Validators.required
]),
customerType: new FormControl('', Validators.required),
firstName: new FormControl('', [Validators.required, Validators.minLength(80)]),
lastName: new FormControl('', [Validators.required, Validators.minLength(80)]),
phone: new FormControl('', [Validators.required, Validators.maxLength(10), Validators.minLength(10)]),
extension: new FormControl('', [Validators.maxLength(7)])
});
// this outputs the entire json
console.log(JSON.stringify(this.trackerForm.value));
//How do I ONLY console.log one of the values? date?
HTML 页 -
<form [formGroup]="trackerForm" (ngSubmit)="onSubmit(trackerForm.value)" novalidate>
<div>
<label class="form-control"><span>{{trackerForm.date.value}} </span></label>
</div>
您需要通过 trackerForm.controls
对象访问 formGroup
控件。
"date"
控件值 trackerForm.controls['date'].value
的示例。
<form [formGroup]="trackerForm" (ngSubmit)="onSubmit(trackerForm.value)" novalidate>
<div>
<label class="form-control"><span>{{trackerForm.get('date').value}} </span></label>
</div>
或
<label class="form-control"><span>{{trackerForm.controls['date'].value}} </span></label>
但第一个肯定更好,因为 'AOT' 不编译第二个。
但我会创建一个组件 getter
以使其更好:
get dateControl(){
return this.trackerForm.get('date');
}
然后:
<form [formGroup]="trackerForm" (ngSubmit)="onSubmit(trackerForm.value)" novalidate>
<div>
<label class="form-control"><span>{{dateControl.value}} </span></label>
</div>