无法在AngularJS中动态获取无线电选择设置的值

时间:2015-05-26 20:19:17

标签: javascript angularjs html5 angularjs-scope

以下是我的代码,其中我没有为每个相应的行选择无线电选项,让我知道我在这里做错了什么。

我的Plnkr代码 - http://plnkr.co/edit/MNLOxKqrlN5ccaUs5gpT?p=preview

虽然我得到了classes对象的名字,但没有得到选择。

HTML code -

  <body ng-controller="myCtrl">
    <div class="container-fluid">
       <form name="formValidate" ng-submit="submitForm()" novalidate="" class="form-validate form-horizontal">
          <div class="form-group">
             <label class="col-sm-2 control-label">Name</label>
             <div class="col-sm-6">
                <input type="text" name="name" required="" ng-model="classes.name" class="form-control" />
             </div>
          </div>
          <div class="form-group">
             <table id="datatable1" class="table table-striped table-hover">
                <tr class="gradeA" ng-repeat="cls in reqgrps">
                   <td ng-bind="cls.name"></td>
                   <td><input type="radio" name="groupName[{{$index}}]" ng-model="classes.satisfies"> Choice 1</td>
                   <td><input type="radio" name="groupName[{{$index}}]" ng-model="classes.satisfies"> Choice 2</td>
                   <td><input type="radio" name="groupName[{{$index}}]" ng-model="classes.satisfies"> Choice 3</td>
                </tr>
             </table>
          </div>
          <div class="panel-footer text-center">
             <button type="submit" class="btn btn-info">Submit</button>
          </div>      
       </form>
    </div>

    <div class="result">{{classes}}</div>
  </body>

脚本文件 -

var myApp = angular.module('myApp', []);

myApp.controller('myCtrl', function($scope){
  $scope.reqgrps = [{name: 'Sub1', roll: 121},{name: 'Sub2', roll: 122}, {name: 'Sub3', roll: 123}];
  $scope.classes = {};
  $scope.result = {};

  $scope.submitForm = function() {
    $scope.result = $scope.classes;
  };

});

-------------编辑-------------

预期产出 -

班级obj -

{
    name: "Test Class",
    satisfies: [
        "Sub1": "Choice 1",
        "Sub2": "Choice 3",
        "Sub3": "Choice 2",
        .................
        ..................
        ..................
        ..................
        "Subn": "Choice 2",
    ]
}

3 个答案:

答案 0 :(得分:3)

您需要区分ng-repeat生成的每一行。

您可以通过向每个ng模型添加[$ index]来实现此目的:

^A

正如其他人所提到的,您可以根据需要使用ng-value设置传递给模型的值,从而使结果动态化。

结果对象是这样的:

<td><input type="radio" ng-model="classes.satisfies[$index]" value="Choice 1"> Choice 1</td>
<td><input type="radio" ng-model="classes.satisfies[$index]" value="Choice 2"> Choice 2</td>
<td><input type="radio" ng-model="classes.satisfies[$index]" value="Choice 3"> Choice 3</td>

See plunker here

答案 1 :(得分:1)

您应该为每一行指定不同的ng-model属性。 (不确定为什么你要在3个相同的行上指定相同的模型)。理论上你不必这样做,但正如我所说,我不明白你为什么会这样做。

您还应在单选按钮上添加值属性:

http://plnkr.co/edit/JN4JuQJH2OvRxoawfDbv?p=preview

来自角度文档:

  

值字符串
  选中时应设置表达式的值。

https://docs.angularjs.org/api/ng/input/input%5Bradio%5D

我还建议删除控制器中空对象的初始化(如果ng-model没有找到它将为你创建它的范围内的属性),我已经注意到你&# 39;如果你不知道这只是双括号的捷径,那就使用了ng-bind:{{}}

编辑:

如果您的值需要是动态值,您可以使用ng-value并在范围上指定一个属性,然后可以在控制器中设置该属性

答案 2 :(得分:1)

您需要为每个单选按钮设置ng-value,因此Angular将能够选择这些值。你有3个相同的行,所以我为它们添加了一些虚拟值来显示正确的输出。

http://plnkr.co/edit/AxUx83xdotniYru6amGU?p=preview

此外,您可以在官方文档中找到使用Angular单选按钮的明确示例:

https://docs.angularjs.org/api/ng/input/input%5Bradio%5D

更新:

检查已编辑的plnkr,希望它有所帮助!