2 回答

TA貢獻1812條經(jīng)驗 獲得超5個贊
您可以使用 patchValue 來更改表單控件的值
yourFormGroupName.controls.yourFormControlName.patchValue(您要分配的值)
在你的情況下
this.sendYearsForm = this.fb.group({
year: [null]
});
// Show the current year with -5 and +3
this.selectedYear = this.currentDate.getFullYear(); // The current year is transferred here. How do I integrate it into the template?
this.sendYearsForm.controls.year.patchValue(this.selectedYear); // this will set the value of 'year' control to selectedYear(current year)
for (let i = this.currentDate.getFullYear() - 5; i <= this.currentDate.getFullYear() + 3; i++) {
this.sendYears.push(i);
}

TA貢獻1853條經(jīng)驗 獲得超9個贊
您可以通過以下方式之一執(zhí)行此操作:
this.selectedYear = this.currentDate.getFullYear();
this.sendYearsForm= this.fb.group({
year: new FormControl(this.selectedYear, [])
});
或者
this.sendYearsForm = this.fb.group({
year: [null]
});
this.selectedYear = this.currentDate.getFullYear();
this.sendYearsForm.get('year').setValue(this.selectedYear);
添加回答
舉報