第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Reactive Forms 中將 Select 中的當(dāng)前年份設(shè)置為默認值?

如何在 Reactive Forms 中將 Select 中的當(dāng)前年份設(shè)置為默認值?

繁星coding 2022-07-15 10:07:42
您好,我創(chuàng)建了一個選擇,顯示當(dāng)前年份、過去 5 年和未來 3 年。現(xiàn)在我想設(shè)置它,以便當(dāng)前年份始終是默認值。這應(yīng)該在反應(yīng)形式中完成......你知道怎么做嗎?我的代碼:// HTML<form [formGroup]="sendYearsForm"><select class="upload_slc" id="upload_slc" required title="" formControlName="year"  (change)="sendYear($event)">   <option *ngFor="let years of sendYears" [value]="years">{{years}}</option></select> <div class="select_arrow"></div></form>// TSsendYearsForm: FormGroup;currentDate: Date = new Date();selectedYear: number;// Year form    this.sendYearsForm = this.fb.group({      year: [null]    });    // Show the current year with -5 and +3    this.selectedYear = this.currentDate.getFullYear(); // HThe current year is transferred here. How do I integrate it into the template?    for (let i = this.currentDate.getFullYear() - 5; i <= this.currentDate.getFullYear() + 3; i++) {      this.sendYears.push(i);    }
查看完整描述

2 回答

?
ABOUTYOU

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);

}


查看完整回答
反對 回復(fù) 2022-07-15
?
暮色呼如

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);


查看完整回答
反對 回復(fù) 2022-07-15
  • 2 回答
  • 0 關(guān)注
  • 155 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號