javascript - 将 ng-model 与多个 ng-repeated 单选按钮组一起使用

标签 javascript angularjs

参见JsBin

我有一个由ng-repeat生成的动态检查点列表,每个项目都有六个单选按钮。我需要将每个集合绑定(bind)到 $scope 对象。

在下面,您可以看到我已将名称设置为 selectedOption1selectedOption2 等...这允许每个 ng-repeated 列表独立于下一个。现在,我需要将这些 selectedOptionX 组的选定选项绑定(bind)到 $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>

我也尝试过其他一些方法,但没有什么可以接近的。

最佳答案

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>

关于javascript - 将 ng-model 与多个 ng-repeated 单选按钮组一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25060842/

相关文章:

javascript 在基于 Rails 3.1 Assets 的应用程序中包含两次

javascript - IndexedDB 作为 Angular 服务

javascript - Dropzone.js 拖放在 IE10 中不起作用

javascript - HTML5 Canvas 绘画逼真简单铅笔

javascript - 防止 iframe 安全问题

javascript - Websocket 在刷新时不连接

javascript - Angular中多层指令之间的通信

javascript - AngularJS 显示带有子元素的 div

angularjs - 使用 TypeScript 在 AngularJS 服务中进行依赖注入(inject)的最佳实践

javascript - 如何显示在过滤器的 ng 重复中找不到匹配项的错误消息