1 回答

TA贡献2036条经验 获得超8个赞
这些不是复选框而是单选按钮。假设您确实需要单选按钮(在您的情况下它看起来像这样,因为它是其中之一),则需要完成一些事情。
您可以使用 1 个属性来实现此目的,而不是使用 2 个属性来指示选择了哪个选项。
所以
this.otpEmailValue = 1;
this.otpCellValue = 0;
成为
this.contact = 'email'; // This line is now equivalent to the ones above
在模板中,单选按钮输入需要具有相同的名称才能充当 1 个输入而不是 2 个输入,因为毕竟我们只想选择 1 个选项。该ngModel指令现在指向我们想要绑定的值,在我们的例子中是contact。最后,该值应该是静态的。当绑定的属性值ngModel与单选按钮之一的值匹配时,它将选择它。
因此,在所有这些更改之后,我们得到以下结果。
<input type="radio"
name="contact-option"
id="1"
class="with-gap"
[(ngModel)]="contact"
value="cell"> Cell
<input type="radio"
name="contact-option"
id="2"
class="with-gap"
[(ngModel)]="contact"
value="email"> Email
添加回答
举报