使用具有多个ng-repeat单选按钮组的ng-model

时间:2014-07-31 13:59:34

标签: javascript angularjs

请参阅JsBin

我有checkpoints的动态列表,它是从ng-repeat生成的,每个项目都有六个单选按钮。我需要将每个集合绑定到$scope对象。

下面,您可以看到我已将名称设置为selectedOption1selectedOption2等内容......这样每个ng-repeated列表都可以独立于下一个selectedOptionX列表。现在,我需要将这些$scope组的选定选项绑定到checkpoint.Name对象,同时仍然在对象中维护<table> <tbody> <tr ng-repeat="checkpoint in checkpoints"> <td>{{checkpoint.Name}}</td> <td><input type="radio" name="selectedOption{{$index}}" value="pass" /></td> <td><input type="radio" name="selectedOption{{$index}}" value="fail" /></td> <td><input type="radio" name="selectedOption{{$index}}" value="remediated" /></td> <td><input type="radio" name="selectedOption{{$index}}" value="nc" ng-checked="true" /></td> <td><input type="radio" name="selectedOption{{$index}}" value="na" /></td> <td></td> <td></td> </tr> </tbody> </table>

[
    {
        Name: "the first checkpoint",
        Value: "remediated"
    },
    {
        Name: "the second checkpoint",
        Value: "fail"
    },
    {
        Name: "the third checkpoint",
        Value: "pass"
    },
];

输出对象可能类似于:

<td><input type="radio" name="selectedOption{{$index}}" value="pass" ng-model="selectedOption{{$index}}"/></td>

//and
<td><input type="radio" name="selectedOption{{$index}}" value="pass" ng-model="selectedOption[$index]"/></td>

//and
<td><input type="radio" name="selectedOption{{$index}}" value="pass" ng-model="selectedOption.$index"/></td>

//and
<td><input type="radio" name="selectedOption{{$index}}" value="pass" ng-model="checkpoint.Name"/></td>

我尝试过的......

{{1}}

我也尝试了其他一些事情,但没有任何结果。

2 个答案:

答案 0 :(得分:2)

http://jsbin.com/haxor/1/edit

<table>
      <tbody>
        <tr ng-repeat="checkpoint in checkpoints">
          <td>{{checkpoint.Name}}</td>
          <td><input type="radio" ng-model="checkpoint.selectedOption" value="pass" name="selectedOption{{$index}}"></td>
          <td><input type="radio" ng-model="checkpoint.selectedOption" value="fail" name="selectedOption{{$index}}"></td>
          <td><input type="radio" ng-model="checkpoint.selectedOption" value="remediated" name="selectedOption{{$index}}"></td>
          <td><input type="radio" ng-model="checkpoint.selectedOption" value="nc"  name="selectedOption{{$index}}" ng-checked="true"></td>
          <td><input type="radio" ng-model="checkpoint.selectedOption" value="nc" ng-checked="true"  name="selectedOption{{$index}}"></td>
          <td><input type="radio" ng-model="checkpoint.selectedOption" value="x"  name="selectedOption{{$index}}"></td>
          <td></td>
          <td></td>
        </tr>
      </tbody>
    </table>

答案 1 :(得分:1)

试试这个,它会在检查点对象

中设置选定的值

http://jsbin.com/barufuhi/3/edit

<div ng-controller="CheckpointsController">
<table>
  <tbody>
    <tr ng-repeat="checkpoint in checkpoints">
      <td>{{checkpoint.Name}}</td>
      <td><input type="radio" ng-model="checkpoint.value" value="one"></td>
      <td><input type="radio" ng-model="checkpoint.value" value="two"></td>
      <td><input type="radio" ng-model="checkpoint.value" value="three"></td>
      <td><input type="radio" ng-model="checkpoint.value" value="four"></td>
      <td><input type="radio" ng-model="checkpoint.value" value="five"></td>
      <td><input type="radio" ng-model="checkpoint.value" value="six"></td>
      <td></td>
      <td></td>
    </tr>
  </tbody>
</table>
</div>