【问题标题】:Radio group selected option not updating ngModel value in angular单选组选择的选项不更新 ngModel 角度值
【发布时间】:2014-05-12 03:45:42
【问题描述】:

我是 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 问题的链接或新答案。提前致谢。

【问题讨论】:

  • 循环中有arr,那么为什么要使用dragDropOption[$index]
  • @Yoshi 是 "arr.text" 而不是 "dragDropOption[$index].text" 这是在小提琴上调试代码。

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


【解决方案1】:

input

data-ng-model="option"

到:

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

演示Fiddle

【讨论】:

  • 也可以返回$scope.option = $scope.dragDropOption[0].text;
【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-02
    • 2017-05-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-14
    • 1970-01-01
    相关资源
    最近更新 更多