2016-10-04 98 views
0

我該如何同步單選按鈕的下拉菜單,所以當我檢查一個按鈕,下拉菜單動態改變相同的選項?如何用單選按鈕綁定無線電下拉菜單?

var app = angular.module('myApp', []); 
 
app.controller('myCtrl', function($scope) { 
 
    $scope.colors = [ 
 
     {model : "Red"}, 
 
     {model : "Green"}, 
 
     {model : "Blue"}, 
 
     {model : "Purple"} 
 
    ]; 
 
});
<html> 
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
 
<body> 
 

 
<div ng-app="myApp" ng-controller="myCtrl"> 
 

 
<p>What is your favorite color?</p> 
 

 
<select ng-model="selectedColor" ng-options="x.model for x in colors"> 
 
</select> 
 
<input type="radio" ng-model="colors.model" value = "{{selectedColor.model}}"> Red 
 
<input type="radio" ng-model="colors.model" value = "{{selectedColor.model}}"> Green 
 
<input type="radio" ng-model="colors.model" value = "{{selectedColor.model}}"> Blue 
 
<input type="radio" ng-model="colors.model" value = "{{selectedColor.model}}"> Purple 
 

 
</div> 
 

 
</body> 
 
</html>
感謝

+0

,也反之亦然 – blooddrunk

+0

所以你想改變下拉菜單的值取決於單選按鈕更新其他值? –

+0

您是否檢查過:https://docs.angularjs.org/api/ng/directive/ngValue? – Wandrille

回答

1

的方法之一是使用相同的ng-model這意味着你要考慮通過相應的值應爲無線電什麼,這將是該數組中的對象

var app = angular.module('myApp', []); 
 
app.controller('myCtrl', function($scope) { 
 
    $scope.colors = [ 
 
     {model : "Red"}, 
 
     {model : "Green"}, 
 
     {model : "Blue"}, 
 
     {model : "Purple"} 
 
    ]; 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
 

 
<div ng-app="myApp" ng-controller="myCtrl"> 
 

 
<p>What is your favorite color?</p> 
 

 
<select ng-model="selectedColor" ng-options="x.model for x in colors"> 
 
</select> 
 
<input name='rad' type="radio" ng-model="selectedColor" ng-value="colors[0]"> Red 
 
<input name='rad' type="radio" ng-model="selectedColor" ng-value="colors[1]" > Green 
 
<input name='rad' type="radio" ng-model="selectedColor" ng-value="colors[2]"> Blue 
 
<input name='rad' type="radio" ng-model="selectedColor" ng-value="colors[3]"> Purple 
 

 
</div>

種一些其他方法將使用ng-change對每更新其他內部功能,或在不同的ng-model使用的手錶,並在這些手錶

+0

如何添加一個輸入框供用戶輸入他們的選擇?例如,如果用戶類型2將是綠色和4紫色等等?並相應地更改單選按鈕和下拉菜單? – blooddrunk

相關問題