我正在使用ng-repeat显示角度列表视图。我能够显示列表。实际上每行都有一个单选按钮。我在屏幕上有一个按钮。我想得到所选单选按钮的对象按钮点击。换句话说如果我选择第一行单选按钮然后如果我点击按钮我需要得到它对象我将如何实现这一点?
这是我的代码
<table>
<tr id="$index"ng-repeat= "a in data " ng-click="getselectedRow(a)">
<td> {{a.condidateID}} </td>
<td> {{a.condidateName}} </td>
<td> {{a.territory}} </td>
<td> {{a.voteCount}} </td>
<td> <input type="radio" name="chickenEgg" value="egg" ng-model="chekratiovalue"></td>
</tr>
</table>
<button style="left: 40%;top: 40%;float: left;position: relative" ng-click="voteForPerson()">Vote</button>
更新
function userdetail($scope,$http,$location,$routeParams){
console.log(JSON.parse($routeParams.userDetail));
var userDetail=JSON.parse($routeParams.userDetail);
$scope.data=userDetail.data;
console.log($scope.data);
console.log($scope.data);
$scope.changeEvnt = function(index) {
$scope.activeRow = index;
alert( $scope.activeRow);
}
$scope.voteForPerson = function() {
var selcted = $scope.data[activeRow ];
}
}
<table>
<tr id="$index"ng-repeat= "a in data " ng-click="getselectedRow(a)">
<td> {{a.condidateID}} </td>
<td> {{a.condidateName}} </td>
<td> {{a.territory}} </td>
<td> {{a.voteCount}} </td>
<td> <input type="radio" name="chickenEgg" value="egg" ng-model="chekratiovalue" ng-change="changeEvnt($index)"></td>
</tr>
</table>
<button style="left: 40%;top: 40%;float: left;position: relative" ng-click="voteForPerson()">Vote</button>
答案 0 :(得分:1)
添加ng-change
指令,
<input type="radio" name="chickenEgg" value="egg" ng-model="chekratiovalue" ng-change="chengeEvnt($index)">
在控制器中
$scope.chengeEvnt = function(index) {
$scope.activeRow = index; // get the index when changing the radio button
}
$scope.voteForPerson = function() {
var selected = $scope.data[$scope.activeRow]; // get the row of selected radio button using the `$scope.activeRow` function
}
答案 1 :(得分:1)
单击ng-repeat中的单选按钮时,可以使用$ parent表示法。
<table>
<tr id="$index" ng-repeat= "a in data ">
<td> {{a.condidateID}} </td>
<td> {{a.condidateName}} </td>
<td> {{a.territory}} </td>
<td> {{a.voteCount}} </td>
<td> <input type="radio" name="chickenEgg" value="{{a.chekratiovalue}}" ng-model="$parent.chekratiovalue"></td>
</tr>
</table>
这是必需的,因为ng-repeat会创建自己的范围。
答案 2 :(得分:0)