我希望单选按钮的默认值是 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>
插入到gift.k_gift 的值是1
。我试过 [value] = 1
,也试过 k_gift: [1, [Validators.required, CustomValidators.gt(0)]],
还尝试了 html checked
标签中的 input
。
没有一个对我有用。
如何将此单选按钮的默认值设置为 checked
?
答案 0 :(得分:1)
您正在使用模型驱动的表单。不要在模板中使用 [value]
输入,而是相应地设置模型:
this._giftService.getUserGifts()
.subscribe(g => {
// here
this.gifts = g;
this.form.patchValue({ k_gift: /* insert desired value */ });
},