1 javascript angular angular-reactive-forms angular-forms
我不知道该怎么办
这是我的组件打字稿,带有新的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?
Run Code Online (Sandbox Code Playgroud)
页面上的HTML-
<form [formGroup]="trackerForm" (ngSubmit)="onSubmit(trackerForm.value)" novalidate>
<div>
<label class="form-control"><span>{{trackerForm.date.value}} </span></label>
</div>
Run Code Online (Sandbox Code Playgroud)
<form [formGroup]="trackerForm" (ngSubmit)="onSubmit(trackerForm.value)" novalidate>
<div>
<label class="form-control"><span>{{trackerForm.get('date').value}} </span></label>
</div>
Run Code Online (Sandbox Code Playgroud)
要么
<label class="form-control"><span>{{trackerForm.controls['date'].value}} </span></label>
Run Code Online (Sandbox Code Playgroud)
但是第一个绝对更好,因为“ AOT”不会编译第二个。
但我会创建一个组件getter使其变得更好:
get dateControl(){
return this.trackerForm.get('date');
}
Run Code Online (Sandbox Code Playgroud)
然后 :
<form [formGroup]="trackerForm" (ngSubmit)="onSubmit(trackerForm.value)" novalidate>
<div>
<label class="form-control"><span>{{dateControl.value}} </span></label>
</div>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4192 次 |
| 最近记录: |