无线电组选择的选项不以角度更新ngModel值

时间:2014-04-01 11:35:16

标签: javascript angularjs angularjs-ng-repeat radio-group angular-ngmodel

我是angularjs的新手,并试图找到一个解决方案,用于将选定的无线电组值设置为ngmodel。

//my.html     

<div ng-controller='controller'> 
 <div class="btn-group" ng-model="option" ng-repeat="arr in dragDropOption">
  <input type="radio" name="optionCorrectOpt" data-ng-model="option" 
    value="{{dragDropOption[$index].text}}">
      {{dragDropOption[$index].text}}
</div>

和      //mycontroller.js

 app = angular.module('app', []);

 app.controller('controller', function ($scope) {
 $scope.dragDropOption = [];
 $scope.option = "not set";

 $scope.dragDropOption = [{
     text: "analog"
 }, {
     text: "isdn"
 }, {
     text: "dsl"
 }];
 //   $scope.option = $scope.dragDropOption[0].text;
});

My fiddle is here

可能是重复的问题,请帮我分享已经回答的stackoverflow问题的链接或新的答案。提前谢谢。

2 个答案:

答案 0 :(得分:2)

替换

$scope.option = "not set";

<input type="radio" name="optionCorrectOpt" data-ng-model="option" 
value="{{dragDropOption[$index].text}}">

致:

 $scope.radioOption = {};
 $scope.radioOption.selected = "not set"

 <input type="radio" name="optionCorrectOpt" data-ng-model="radioOption.selected" 
value="{{dragDropOption[$index].text}}">

JS FIDDLE

答案 1 :(得分:2)

input更改

data-ng-model="option"

为:

data-ng-model="$parent.option"

演示 Fiddle