2014-10-28 78 views
0

我正在使用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

您需要$指数传递给你的点击功能。 – user2808895 2014-10-28 05:56:40

+0

在哪里通过?按钮是没有屏幕和每行单选按钮。一次只选择一个单选按钮 – user944513 2014-10-28 05:57:58

回答

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 
} 
+0

它第一次工作...等待我会告诉你发生了什么 – user944513 2014-10-28 06:01:40

+0

请检查更新..我会应用你的anser – user944513 2014-10-28 06:04:03

+0

请检查我的更新回答:) – 2014-10-28 06:05:17

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会创建它自己的范围。

Working Example

+0

@ user944513:这有帮助吗? – V31 2014-11-04 06:58:47

0

你只需要使用

ng-value="item" 

,你可以让你从那里想要的任何信息。

这里有一个plunker