我希望单选按钮的默认值为checked
。我尝试了以下操作,但没有成功。
form: FormGroup;
gifts: IGift;
this.form = this.fb.group({
phone: ['', [Validators.required, Validators.pattern('^[0][5][0-9]{8}$')]],
email: ['', [CustomValidators.email]],
k_gift: ['', [Validators.required, CustomValidators.gt(0)]],
});
this._giftService.getUserGifts()
.subscribe(g => {
this.gifts = g;
},
error => {
if (error.includes("401")) {
localStorage.removeItem("token");
location.reload();
})
<input type="radio" [value]="gifts.k_gift" aria-checked="false" attr.id="gift{{gift.k_gift}}" formControlName="k_gift" >
<label class='control-label' attr.for="gift{{gift.k_gift}}">
{{gift.desc_k_gift}}
</label>
插入礼品的值。k_gift是1
。我试过[value] = 1
,也试过k_gift: [1, [Validators.required, CustomValidators.gt(0)]],
也尝试了checked
在htmlinput
标签。
都不适合我。如何将此单选按钮的默认值设置为checked
?
您正在使用模型驱动的表单。在模板中不使用[value]
输入,而是相应地设置模型:
this._giftService.getUserGifts()
.subscribe(g => {
// here
this.gifts = g;
this.form.patchValue({ k_gift: /* insert desired value */ });
},