具有ng-repeat和单选按钮的Angular JS表

时间:2013-08-23 14:56:25

标签: angularjs html-table radio-button angularjs-ng-repeat

我正在尝试使用ng-repeat创建一个表,每个行的开头都有一个单选按钮选择器。该表如下所示:

<table>
     <tbody>  
         <tr ng-repeat="model in modelList">
              <td>
                 <input type="radio" ng-model="model.select"></input>
              </td>
             <td>{{ model.NDPName }}</td>
             <td>{{ model.OEM }}</td>
             <td>{{ model.version }}</td>
             <td>{{ model.dateAdded }}</td>
             <td>{{ model.validUntil }}</td>
         </tr>
     </tbody>
</table>

ng-repeat从一个看起来像的模型列表中获取:

    $scope.modelList =
    [
        {
            select: false,
            NDPName: "NDP1",
            OEM: "CHOAM Inc.",
            version: "01",
            dateAdded: "Jan 1, 2013",
            validUntil: "Jan 1, 2014",
        },
        {
            select: false,
            NDPName: "NDP2",
            OEM: "Tyrell Corp.",
            version: "01",
            dateAdded: "Jan 1, 2014",
            validUntil: "Jan 1, 2015",
        },
        {
            select: false,
            NDPName: "NDP3",
            OEM: "Stark Industries",
            version: "01",
            dateAdded: "Jan 1, 2015",
            validUntil: "Jan 1, 2016",
        },
        {
            select: false,
            NDPName: "NDP4",
            OEM: "Monsters Inc.",
            version: "01",
            dateAdded: "Jan 1, 2016",
            validUntil: "Jan 1, 2017",
        },
        {
            select: false,
            NDPName: "NDP5",
            OEM: "ACME Corp",
            version: "01",
            dateAdded: "Jan 1, 2017",
            validUntil: "Jan 1, 2018",
        }
    ];

我遇到的问题是单选按钮的行为不像单选按钮。它们各自位于单独的范围内,因此将允许选择表中的多行。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:14)

您需要为单选按钮设置name属性,以便将它们组合在一起。

<input type="radio" name="groupName" ng-model="model.select" />

read about the name property here

答案 1 :(得分:1)

您应该在输入中指定名称标签。然后,所有输入将属于同一组,并将按预期运行:

<input type="radio" name="myGroup" ng-model="model" />