簡體   English   中英

角度二向綁定無線電輸入

[英]Angular two way binding radio inputs

我有兩組無線電輸入:

<input type="radio" value="0" ng-model="domestic" ng-checked="domestic"> Domestic
<input type="radio" value="1" ng-model="international" ng-checked="international"> International

和:

<input type="radio" value="0" ng-model="domestic" ng-checked="domestic"> Verified
<input type="radio" value="1" ng-model="international" ng-checked="international"> Unverified

不管我選擇哪個輸入中的哪個輸入,我都希望它相應地更改另一個輸入中的相同輸入。

我之所以這樣寫,是因為我希望它可以雙向工作。

這是綁定的正確方法嗎? 或者,還有更好的方法。

謝謝

從抽象的角度考慮它。 您現在正在做的是將兩個無線電輸入綁定到相同的值。 如果您希望兩個無線電輸入在所有情況下都代表相同的數據值,那么您可以做的很好。

否則,您始終可以將ng-checked設置為功能。 如果您有其他需要考慮的因素,這將是理想的選擇。 就像是:

<input type="radio" value="0" ng-model="domestic" ng-checked="domesticChecked()"> Domestic

使用:

$scope.domesticChecked = function(){
    // you could do other checks here as well if it needed to be more complicated
   return (domestic && someOtherConsideration) ? true : false
 }

簡而言之,如果您的應用程序中的所有方案在這兩個復選框之間都呈現1:1的值關聯(如果選中了一個,則始終也選中了另一個,反之亦然),那么您所擁有的就可以了。

我想你要找的是

<input type="radio" value="0" ng-model="domestic" name="x"> Domestic
<input type="radio" value="1" ng-model="domestic" name="x"> International


<input type="radio" value="0" ng-model="domestic" name="y"> Verified
<input type="radio" value="1" ng-model="domestic" name="y"> Unverified

演示: 小提琴

您需要為單選元素分配名稱,以便可以對它們進行分組,然后,如果使用相同的ng-model ,就可以了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM