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
添加回答
舉報