简体   繁体   English

在Angular.js中更新单选按钮ngModel

[英]Updating ngModel of radio buttons in Angular.js

I am trying to implement a web-based table with Angular.js such that each row can be selected with a radio button. 我正在尝试使用Angular.js实现基于Web的表,以便可以通过单选按钮选择每一行。 The rows can be swapped up or down and the selection state should be swapped together with the rows. 行可以上下交换,选择状态应与行一起交换。 So far, new rows can be added, the row swapping works just fine, and the scope variable of the selected row index is updated upon user's click on radio buttons. 到目前为止,可以添加新行,行交换可以正常工作,并且在用户单击单选按钮时更新所选行索引的作用域变量。

The problem that I encountered is that the selected state of radio buttons and the scope variable is not in sync. 我遇到的问题是单选按钮和范围变量的选定状态不同步。 More specifically, while the scope variable is correctly updated during the row swapping, the radio buttons are usually deselected after swapping. 更具体地说,虽然在行交换期间正确更新了范围变量,但通常在交换后取消选择单选按钮。 It can also happen that the selected radio button is different from the index in the scope variable. 还可能发生所选单选按钮与范围变量中的索引不同的情况。

Any suggestions on how to fix this problem would be welcome! 任何有关如何解决此问题的建议都将受到欢迎!

A JSFiddle of what I have implemented so far can be found here: 我到目前为止已实现的JSFiddle可以在这里找到:

http://jsfiddle.net/7jeume9r/38/ http://jsfiddle.net/7jeume9r/38/

HTML Code HTML代码

<div ng-controller="SelectTableController">
<table id="select-table">
<thead>
    <tr>
        <th> Text </th>
        <th> Select? </th>
        <th> </th>
        <th> </th>
    </tr>
</thead>
<tbody>
    <tr ng-repeat="x in entries">
        <td> {{x['text']}} (id: {{$index}}) </td>
        <td> <input type="radio" ng-model="$parent.selectedchoice" name="selectedchoice" value="{{$index}}"/> </td>
        <td>
            <button type="button" ng-click="moveUpEntry(x)" ng-show="$index > 0"> Move up </button> 
            <button type="button" ng-click="moveDownEntry(x)" ng-show="$index < entries.length-1"> Move down </button>
        </td>
    </tr>
</tbody>
</table>

<div>
    <input type="text" ng-model="newtext"> </input>
    <button type="button" ng-click="addEntry()"> Add </button>
</div>
<div> <tt> Selected Choice: {{selectedchoice}} </tt> </div>
</div>

Javascript / Angular.js Code Javascript / Angular.js代码

angular.module('DemoApp', []).controller('SelectTableController', function($scope) {
$scope.entries = [{text:"abc"}, {text:"def"}]
$scope.selectedchoice = '-1'

$scope.addEntry = function() {
    var text = $scope.newtext
    $scope.entries.push({text: text})
    $scope.newtext = ""
}

$scope.moveUpEntry = function(entry) {
    var index = $scope.entries.indexOf(entry)
    if (index > 0 && index < $scope.entries.length) {
        $scope.entries.swap(index,index-1)
        if ($scope.selectedchoice == index)
            $scope.selectedchoice = index-1
        else if ($scope.selectedchoice == index-1)
            $scope.selectedchoice = index
    }
}

$scope.moveDownEntry = function(entry) {
    var index = $scope.entries.indexOf(entry)
    if (index >= 0 && index < $scope.entries.length-1) {
        $scope.entries.swap(index,index+1)
        if ($scope.selectedchoice == index)
            $scope.selectedchoice = index+1
        else if ($scope.selectedchoice == index+1)
            $scope.selectedchoice = index
    }
}

Array.prototype.swap = function (x,y) {
    var b = this[x];
    this[x] = this[y];
    this[y] = b;
    return this;
}
});

Your radio buttons should be part of the "row" and a radio button is checked with 您的单选按钮应该是“行”的一部分,并且使用

checked="checked"

You can't check them with value as you do 您无法像对待价值那样检查它们

<input type="radio" ng-model="$parent.selectedchoice" name="selectedchoice" value="{{$index}}"/>

I'm trying to fix your jsFiddle atm. 我正在尝试修复您的jsFiddle atm。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM